uva10048 Audiophobia

Root :: Problem Set Volumes (100...1999) :: Volume 9 (900-999) Submit Download as PDF Problem Stats

10048 - Audiophobia

Time limit: 3.000 seconds
  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 60­65 decibels and that of heavy traffic is 70­80 decibels.

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.

To get from crossing A to crossing G you may follow the following path: A­C­F­G. In that case you must be capable of tolerating sound intensity as high as 140 decibels. For the paths A­B­E­GA­B­D­G and A­C­F­D­G you must tolerate respectively 90, 120 and 80 decibels of sound intensity. There are other paths, too. However, it is clear that A­C­F­D­G is the most comfortable path since it does not demand you to tolerate more than 80 decibels.

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 $C (le 100)$$S (le
1000)$ and $Q (le 10000)$ where Cindicates the number of crossings (crossings are numbered using distinct integers ranging from 1 to C), Srepresents the number of streets and Q is the number of queries.

Each of the next S lines contains three integers: c1c2 and d indicating that the average sound intensity level on the street connecting the crossings c1 and c2 ( $c_1 
e c_2$) is d decibels.

Each of the next Q lines contains two integers c1 and c2 ( $c_1 
e c_2$) asking for the minimum sound intensity level you must be able to tolerate in order to get from crossing c1 to crossing c2.

The input will terminate with three zeros form CS 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

本来就是道水题,但是貌似被学弟问道把点数改成10^4,边数改成10^5,询问改成10^5,求任意两点间的路径上的最大值最小怎么做,觉得有必要去拍一下,结果就真是觉得不做死就不会死,最后把两种解法写到了一起,于是代码量就突破天际了,呜呜,哭了。。。。。。

因为可能最后的生成的是森林,那么就用正常的Floyd做,当是一棵生成树的时候,树链剖分就来了,唉,表示我只是来测试的,代码已经不忍目视了。

当然还有一种做法就是模拟kruskal的过程,一条边一条边的加入树中,每加入一次,就判断起点和终点是否在一个集合里, 一旦他们在一个集合里了,那么那条路径中的最大值便是当前加入的这条边的权值,因为加入的边是按照从小到大顺序加入的。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #define maxn 100010
  6 #define inf 0x3f3f3f3f
  7 using namespace std;
  8 int n,m,q;
  9 struct spanning_tree{
 10     int u,v,l;
 11     friend bool operator<(spanning_tree a,spanning_tree b){
 12         return a.l<b.l;
 13     }
 14 }st[maxn];
 15 int fa[maxn],dp[110][110];
 16 void build_spanning_tree(int nn){
 17     for(int i=0;i<=nn;i++) fa[i]=i;
 18 }
 19 int find(int x){
 20     if(x!=fa[x]) fa[x]=find(fa[x]);
 21     return fa[x];
 22 }
 23 void union_set(int x,int y){
 24     int xx=find(x),yy=find(y);
 25     if(xx==yy) return;
 26     fa[xx]=yy;
 27 }
 28 struct edge{
 29     int v,l,next;
 30 }e[maxn*2];
 31 int head[maxn],tol;
 32 void init(){
 33     tol=0;
 34     memset(head,-1,sizeof head);
 35 }
 36 void addedge(int u,int v,int l){
 37     e[tol].v=v,e[tol].l=l,e[tol].next=head[u],head[u]=tol++;
 38 }
 39 void read(){
 40     memset(dp,0x3f,sizeof dp);
 41     for(int i=0;i<m;i++){
 42         scanf("%d%d%d",&st[i].u,&st[i].v,&st[i].l);
 43     }
 44     sort(st,st+m);
 45     build_spanning_tree(n);
 46     init();
 47     for(int i=0;i<m;i++){
 48         int x=find(st[i].u),y=find(st[i].v);
 49         if(x!=y){
 50            union_set(x,y);
 51            dp[st[i].u][st[i].v]=dp[st[i].v][st[i].u]=st[i].l;
 52            //cout<<st[i].u<<" "<<st[i].v<<"="<<st[i].l<<endl;
 53            addedge(st[i].u,st[i].v,st[i].l);
 54            addedge(st[i].v,st[i].u,st[i].l);
 55         }
 56     }
 57 }
 58 int size[maxn],dep[maxn],son[maxn],pre[maxn],w[maxn],len[maxn];
 59 int tn;
 60 void dfs(int u,int fa){
 61     size[u]=1,son[u]=u,pre[u]=fa;
 62     for(int i=head[u];i!=-1;i=e[i].next){
 63         int v=e[i].v;
 64         if(v!=fa){
 65            dep[v]=dep[u]+1;
 66            dfs(v,u);
 67            if(size[v]>=size[son[u]]) son[u]=v;
 68            size[u]+=size[v];
 69         }
 70     }
 71 }
 72 void build_tree(int x,int f){
 73     w[x]=tn++,fa[x]=f;
 74     for(int i=head[x];i!=-1;i=e[i].next){
 75         if(e[i].v==son[x]){
 76             len[tn]=e[i].l,build_tree(son[x],fa[x]);
 77             break;
 78         }
 79     }
 80     for(int i=head[x];i!=-1;i=e[i].next){
 81         int v=e[i].v;
 82         if(v!=pre[x]&&v!=son[x]){
 83            len[tn]=e[i].l;
 84            build_tree(v,v);
 85         }
 86     }
 87 }
 88 struct tree{
 89     int l,r,maxv;
 90 }a[maxn*4];
 91 void build(int l,int r,int k){
 92     a[k].l=l,a[k].r=r;
 93     if(l==r){
 94         a[k].maxv=len[l];
 95     }else{
 96         int mid=(l+r)>>1;
 97         build(l,mid,k<<1);
 98         build(mid+1,r,k<<1|1);
 99         a[k].maxv=max(a[k<<1].maxv,a[k<<1|1].maxv);
100     }
101 }
102 int query(int l,int r,int k){
103     if(l<=a[k].l&&a[k].r<=r){
104         return a[k].maxv;
105     }else{
106         int mid=(a[k].l+a[k].r)>>1;
107         if(r<=mid) return query(l,r,k<<1);
108         else if(l>mid) return query(l,r,k<<1|1);
109         else return max(query(l,mid,k<<1),query(mid+1,r,k<<1|1));
110     }
111 }
112 int cal(int x,int y){
113     int ret=0;
114     while(fa[x]!=fa[y]&&x!=y){
115         if(dep[fa[x]]>dep[fa[y]]) swap(x,y);
116         //cout<<fa[x]<<" "<<fa[y]<<endl;
117         ret=max(ret,query(w[fa[y]],w[y],1));
118         y=pre[y];
119     }
120     if(x==y) return ret;
121     if(dep[x]<dep[y]) ret=max(ret,query(w[x]+1,w[y],1));
122     else ret=max(ret,query(w[y]+1,w[x],1));
123     return ret;
124 }
125 bool vis[110];
126 void check_dfs(int u){
127     vis[u]=1;
128     for(int i=head[u];i!=-1;i=e[i].next){
129        if(!vis[e[i].v]) check_dfs(e[i].v);
130     }
131 }
132 bool check_tree(){
133     memset(vis,0,sizeof vis);
134     int ct=0;
135     for(int i=1;i<=n;i++){
136         if(!vis[i]) ct++,check_dfs(i);
137     }
138     return ct==1;
139 }
140 void gao_floyd(int cas){
141     for(int k=1;k<=n;k++){
142        for(int i=1;i<=n;i++){
143           for(int j=1;j<=n;j++){
144                 if(dp[i][k]!=inf&&dp[k][j]!=inf)
145                 dp[i][j]=min(dp[i][j],max(dp[i][k],dp[k][j]));
146           }
147        }
148     }
149     int u,v;
150     if(cas>1) printf("
");
151     printf("Case #%d
",cas);
152     for(int i=0;i<q;i++){
153        scanf("%d%d",&u,&v);
154        if(dp[u][v]==inf) printf("no path
");
155        else printf("%d
",dp[u][v]);
156     }
157 }
158 void solve(int cas){
159     if(!check_tree()){gao_floyd(cas);return;}
160     dep[1]=0,tn=0,len[0]=0;
161     memset(son,0,sizeof son);
162     dfs(1,1);
163     build_tree(1,1);
164     build(0,tn-1,1);
165     int u,v;
166     if(cas>1) printf("
");
167     printf("Case #%d
",cas);
168     for(int i=0;i<q;i++){
169        scanf("%d%d",&u,&v);
170        if(u<1||v<1||u>n||v>n) printf("no path
");
171        else printf("%d
",cal(u,v));
172     }
173 }
174 int main(){
175     //freopen("10048.txt","w",stdout);
176     int cas=1;
177     while(~scanf("%d%d%d",&n,&m,&q)){
178         if(n==0&&m==0&&q==0) break;
179         read();
180         solve(cas++);
181     }
182     return 0;
183 }
uva10048
 
原文地址:https://www.cnblogs.com/wonderzy/p/3439486.html