UVa 10048: Audiophobia
这道题要求我们求出图中的给定的两个节点(一个起点一个终点,但这是无向图)之间所有“路径中最大权值”的最小值,这无疑是动态规划。
我开始时想到根据起点和终点用动态规划直接求结果,但最终由于题中S过大,会超时。
超时的代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <string> #include <algorithm> using namespace std; const int MAX = 1000000; int w[100][100]; int vis[100]; int C,S,Q; int f(int s, int t) { vis[t]=1; int minb=MAX,tmp; for(int i=0; i<C; i++) if(!vis[i] && w[t][i]!=-1) { if(i==s) { minb=w[t][i]; continue;} tmp = f(s,i); tmp = (tmp > w[t][i] ? tmp : w[t][i]); minb = (minb < tmp ? minb : tmp); } vis[t]=0; return minb; } int main() { int c1,c2; int Case=0; while(cin >> C >> S >> Q && C!=0) { memset(w,-1,sizeof(w)); for(int i=0; i<S; i++) { cin >> c1 >> c2; cin >> w[c1-1][c2-1]; w[c2-1][c1-1]=w[c1-1][c2-1]; } if(Case) cout << endl; cout << "Case #" << ++Case << endl; while(Q--) { memset(vis,0,sizeof(vis)); cin >> c1 >> c2; int ans = f(c1-1,c2-1); if(ans==MAX) cout << "no path\n"; else cout << ans << endl; } } return 0; }
被判超时后想到,由于S过大,即要求的起始节点对过多,最好一次性全部求出来。在uva的board中看到别人有使用Floyd_WarShall算法的,受到启发,就自己思考了一下。
Floyd_WarShall算法本身肯定无法完成这题的解答,但只要改写其更新节点值的式子就可以解决这道题。我将更改后的算法命名为Floyd_WarShallEx,代码如下:
void Floyd_WarShallEx() { for(int k=0; k<C; k++) { for(int i=0; i<C; i++) { for(int j=0; j<C; j++) { w[i][j] = (w[i][j] < (w[i][k]>w[k][j]?w[i][k]:w[k][j]) ? w[i][j] : (w[i][k]>w[k][j]?w[i][k]:w[k][j])); } } } }
其中C为图中节点数,w[i][j]表示节点对i,j之间题目所要求的结果。通过下式更新w[i][j]的值:
w[i][j] = (w[i][j] < (w[i][k]>w[k][j]?w[i][k]:w[k][j]) ? w[i][j] : (w[i][k]>w[k][j]?w[i][k]:w[k][j]));
至此,调用Floyd_WarShallEx函数就可以完成所有节点对题目要求的结果的计算。
完整的解题代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <string> #include <algorithm> using namespace std; const int MAX = 1000000; int w[100][100]; int vis[100]; int C,S,Q; void Floyd_WarShallEx() { for(int k=0; k<C; k++) { for(int i=0; i<C; i++) { for(int j=0; j<C; j++) { w[i][j] = (w[i][j] < (w[i][k]>w[k][j]?w[i][k]:w[k][j]) ? w[i][j] : (w[i][k]>w[k][j]?w[i][k]:w[k][j])); } } } } int main() { int c1,c2; int Case=0; while(cin >> C >> S >> Q && C!=0) { for(int i=0; i<C; i++) for(int j=0; j<C; j++) w[i][j]=MAX; for(int i=0; i<S; i++) { cin >> c1 >> c2; cin >> w[c1-1][c2-1]; w[c2-1][c1-1]=w[c1-1][c2-1]; } Floyd_WarShallEx(); if(Case) cout << endl; cout << "Case #" << ++Case << endl; while(Q--) { cin >> c1 >> c2; if(w[c1-1][c2-1]==MAX) cout << "no path\n"; else cout << w[c1-1][c2-1] << endl; } } return 0; }
附上题目:
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: ACFG. In that case you must be capable of tolerating sound intensity as high as 140 decibels. For the paths ABEG, ABDG and ACFDG you must tolerate respectively 90, 120 and 80 decibels of sound intensity. There are other paths, too. However, it is clear that ACFDG 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 , and where C indicates the number of crossings (crossings are numbered using distinct integers ranging from 1 to C), S represents the number of streets and Q is the number of queries.
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 ( ) is d decibels.
Each of the next Q lines contains two integers c1 and c2 ( ) 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 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