UVa10048_Audiophobia (Shortest Path/floyd), graph theory, and Its Application
Solution report
Question:
Find the path with the smallest dB of all paths.
Ideas:
Similar to the concept of the floyd algorithm, u-> v can have another k, which can be used through u-> k-> v, compare the maximum values of u-> k and k-> v with that of u-> v, and save the minimum value.
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#define inf 0x3f3f3f3fusing namespace std;int n,m,q,mmap[110][110];void floyd() { for(int k=0; k<n; k++) for(int i=0; i<n; i++) for(int j=0; j<n; j++) mmap[i][j]=min(mmap[i][j],max(mmap[i][k],mmap[k][j]));}int main() { int i,j,u,v,w,k=1; while(~scanf("%d%d%d",&n,&m,&q)) { if(!n&&!m&&!q)break; for(i=0; i<n; i++) { for(j=0; j<n; j++) mmap[i][j]=inf; mmap[i][i]=0; } for(i=0; i<m; i++) { scanf("%d%d%d",&u,&v,&w); mmap[u-1][v-1]=mmap[v-1][u-1]=w; } floyd(); if(k!=1) printf("\n"); printf("Case #%d\n",k++); while(q--) { scanf("%d%d",&u,&v); if(mmap[u-1][v-1]==inf) printf("no path\n"); else printf("%d\n",mmap[u-1][v-1]); } } return 0;}
Consider yourself lucky! Consider yourself lucky to be still breathing and having fun participates in this contest. but we apprehend that limit 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 inDecibelsAnd 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.
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 crossingATo crossingGYou may follow the following path:ACFG. In that case you must be capable of tolerating sound intensity as high as 140 decibels. For the pathsABEG,ABDGAndActpdYou must tolerate respectively 90,120 and 80 decibels of sound intensity. There are other paths, too. However, it is clear thatActpdIs 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 in multiple test cases.
The first line of each test case contains three integers, and whereCIndicates the number of crossings (crossings are numbered using distinct integers ranging from 1C),SRepresents the number of streets andQIs the number of queries.
Each of the nextSLines contains three integers:C1,C2 andDIndicating that the average sound intensity level on the street connecting the crossingsC1 andC2 () isDDecibels.
Each of the nextQLines contains two integersC1 andC2 () asking for the minimum sound intensity level you must be able to tolerate in order to get from crossingC1 to crossingC2.
The input will terminate with three zeros formC,SAndQ.
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 31 2 501 3 602 4 1202 5 903 6 504 6 804 7 705 7 406 7 1401 72 66 27 6 31 2 501 3 602 4 1203 6 504 6 805 7 407 51 72 40 0 0
Sample Output
Case #1806060 Case #240no path80
Miguel Revilla
2000-12-26