Problem B: Audiophobia
Consider yourself lucky! Consider yourself lucky to be still breathing and having fun participating in this contest. But we apprehend that many of your descendants may not have this luxury. For, as you know, we are the dwellers of one of the most polluted cities on earth. Pollution is everywhere, both in the environment and in society and our lack of consciousness is simply aggravating the situation.However, for the time being, we will consider only one type of pollution - the sound pollution. The loudness or intensity level of sound is usually measured in decibels and sound having intensity level 130 decibels or higher is considered painful. The intensity level of normal conversation is 6065 decibels and that of heavy traffic is 7080 decibels.Problem B: Audiophobia |
Consider the following city map where the edges refer to streets and the nodes refer to crossings. The integer on each edge is the average intensity level of sound (in decibels) in the corresponding street.
In this problem, given a city map you are required to determine the minimum sound intensity level you must be able to tolerate in order to get from a given crossing to another.
Input
The input may contain multiple test cases.The first line of each test case contains three integers
Each of the next S lines contains three integers: c1, c2 and d indicating that the average sound intensity level on the street connecting the crossings c1 and c2 (
Each of the next Q lines contains two integers c1 and c2 (
The input will terminate with three zeros form C, S and Q.
Output
For each test case in the input first output the test case number (starting from 1) as shown in the sample output. Then for each query in the input print a line giving the minimum sound intensity level (in decibels) you must be able to tolerate in order to get from the first to the second crossing in the query. If there exists no path between them just print the line ``no path".Print a blank line between two consecutive test cases.
Sample Input
7 9 3 1 2 50 1 3 60 2 4 120 2 5 90 3 6 50 4 6 80 4 7 70 5 7 40 6 7 140 1 7 2 6 6 2 7 6 3 1 2 50 1 3 60 2 4 120 3 6 50 4 6 80 5 7 40 7 5 1 7 2 4 0 0 0
Sample Output
Case #1 80 60 60 Case #2 40 no path 80
Miguel Revilla
2000-12-26
1.minimum spanning tree
2. DFS on each vertex to find distance, O(N^2)
#include<stdio.h>alternative: Floyd-Warshall algorithm, O(N^3)
#include<cstring>
#include<vector>
#include<queue>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
#define MAXN 100
#define MAXM 1000
#define MAXQ 10000
#define INF 2139062143
using namespace std;
int N,M,Q,e,cases=1;
int v1[MAXM],v2[MAXM],w[MAXM],p[MAXN],mate[MAXN][2],query[MAXQ][2],dis[MAXN][MAXN];
bool trav[MAXN];
vector<pair<int,int> >match[MAXN];
struct Node{
Node(int a,int b,int c):v1(a),v2(b),dis(c){}
bool operator <(const Node b)const {return dis>b.dis;}
int v1,v2,dis;
};
void addedge(int a,int b,int c){
if(DBG)printf("addedge %d %d\n",a,b);
v1[e]=a,v2[e]=b,w[e]=c,e++;
}
int find(int v){
if(p[v]==v)return v;
return p[v]=find(p[v]);
}
void unit(int from,int to){
p[find(from)]=p[find(to)];
}
void DFS(int root,int c,int m){
trav[c]=1;
int mm=m,b;
if(DBG)printf("DFS root %d c %d m %d\n",root,c,m);
rep(i,match[c].size()){
if(!trav[match[c][i].first]){
b=match[c][i].first;
if(match[c][i].second>m)mm=match[c][i].second;
dis[root][b]=dis[b][root]=mm;
DFS(root,b,mm);
mm=m;
}
}
trav[c]=0;
}
void ans(){
int a,b,c,d=0;
priority_queue<Node>q;
rep(i,e)q.push(Node(v1[i],v2[i],w[i]));
rep(i,N)p[i]=i,match[i].clear();
while(q.size()&&d<N-1){
a=q.top().v1,b=q.top().v2,c=q.top().dis,q.pop();
if(DBG)printf("a b %d %d\n",a,b);
if(find(a)!=find(b))unit(a,b),
match[a].push_back(make_pair(b,c)),match[b].push_back(make_pair(a,c)),d++;
}
memset(dis,0x7f,sizeof(dis));
rep(i,N){memset(trav,0,sizeof(trav));DFS(i,i,0);}
printf("Case #%d\n",cases++);
rep(i,Q)if(dis[query[i][0]-1][query[i][1]-1]==INF)printf("no path\n");
else printf("%d\n",dis[query[i][0]-1][query[i][1]-1]);
}
int main(){
int a,b,c;
bool ll=0;
while(scanf("%d%d%d",&N,&M,&Q)==3){
if(N+M+Q==0)break;
e=0;
rep(i,M)scanf("%d%d%d",&a,&b,&c),a--,b--,addedge(a,b,c);
rep(i,Q)scanf("%d%d",&query[i][0],&query[i][1]);
if(ll)printf("\n");ll=1;
ans();
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define PI 3.141592653589793
#define INF 2147483647
#define DBG 0
using namespace std;
typedef long long ll;
int cases=1,N;
int dis[100][100];
void allpairs(){
ll c;
rep(k,N){
rep(i,N)
rep(j,N){
c=max(dis[i][k],dis[k][j]);
if(c<dis[i][j])dis[i][j]=c;
}
//if(DBG)rep(i,N){rep(j,N)printf("(%d)%f ",k,dis[i][j]);printf("\n");}
}
}
int main(){
int a,b,c,d,e;
bool ll=0;
while(cin>>N>>b>>c){
if(N==0&&b==0&&c==0)break;
rep(i,100)rep(j,100)if(i!=j)dis[i][j]=INF;
rep(i,b)cin>>a>>d>>e,dis[a-1][d-1]=dis[d-1][a-1]=e;
if(ll)printf("\n");ll=1;
allpairs();
printf("Case #%d\n",cases++);
rep(i,c){
cin>>a>>d;
if(dis[a-1][d-1]<INF)printf("%d\n",dis[a-1][d-1]);
else printf("no path\n");
}
}
}
沒有留言:
張貼留言