# uva 10048 Audiophobia

431人阅读 评论(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 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 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-G,
A-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(≤ 100), S(≤ 1000) and Q(≤ 10000) 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 c1and c2(c1̸= c2) is d decibels.
Each of the next Q lines contains two integers c1and c2(c1̸= c2) asking for the minimum sound
intensity level you must be able to tolerate in order to get from crossing c1to 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

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxint=999999;
int dp[101][101];
int Map[101][101];
int low[101];
bool vis[101];
vector<int> vp;
int c,s,q;
void ini()
{
for(int i=1;i<=c;i++)
for(int j=1;j<=c;j++)
Map[i][j]=maxint;
memset(vis,0,sizeof(vis));
memset(dp,-1,sizeof(dp));
}
struct query
{
int a,b;
};
query que[10001];
void prim(int c)
{
vp.clear();
int pre,min,pos;//pre代表与pos相连的节点,也就是找到pos之前找到的节点
vis[1]=true;
pos=1;
pre=1;
vp.push_back(pos);//首先选择节点1作为起点，节点1加入到vp
for(int i=1;i<=c;i++)
if(i!=pos)
low[i]=Map[pos][i];
for(int i=1;i<c;i++)
{
min=maxint;
min=maxint+1;//改动1，使得能找到非连通节点
for(int j=1;j<=c;j++)
if(!vis[j]&&min>low[j])
{
min=low[j];pos=j;
}
dp[pre][pos]=dp[pos][pre]=min;
for(int k=0;k<vp.size();k++)//改动2，每次更新
dp[pos][vp[k]]=dp[vp[k]][pos]=max(dp[vp[k]][pre],dp[pos][pre]);
vp.push_back(pos);
pre=pos;
vis[pos]=1;
for(int j=1;j<=c;j++)
if(!vis[j]&&low[j]>Map[pos][j])
low[j]=Map[pos][j];
}
}
int main()
{
int a,b,d,k=1;
ios::sync_with_stdio(false);
while(cin>>c>>s>>q)
{
if(c+s+q==0)
return 0;
ini();
for(int i=1;i<=s;i++)
{
cin>>a>>b>>d;
Map[a][b]=Map[b][a]=d;
}
for(int i=1;i<=q;i++)
cin>>que[i].a>>que[i].b;
prim(c);
if(k!=1)
cout<<endl;
cout<<"Case #"<<k++<<endl;
for(int i=1;i<=q;i++)
{
if(dp[que[i].a][que[i].b]==maxint||dp[que[i].a][que[i].b]==-1)
cout<<"no path"<<endl;
else
cout<<dp[que[i].a][que[i].b]<<endl;
}
}
return 0;
}



（说的好絮叨，为了能不让自己过两天忘了）

1.因为数据里给的数据不一定是完全连通的，这就要求我们把数据里的图变成最小连通森林。比如样例2的数据，按照prim算法的思想，找到的节点的顺序是1，2 ,3 , 6 , 4，但是下次在找节点的时候由于没有和12364这5个节点相连的点，所以走到4的时候就卡死了。由于设置邻接矩阵的缘故，每个节点相连的权值最初全被初始化为maxint的值，相当于权值是maxint，所以每次用来更新最小边的min值（代码中的min)保留的还是上次找到的值，比如你上次找到4到5的值是min=maxint，因为所有边的权值都是小于等于maxint的(等于maxint相当于不连通)，所以如果当前找的权值是maxint说明当前节点找到了一个不相连的点，不用去管，更新下次的min为maxint+1就行。

2.

dp[pos][vp[i]]=dp[vp[i]][pos]=max(dp[vp[i]][pre],dp[pos][pre])
vp中保存的全是之前找到的节点。

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
评论排行
最新评论