uva 10048 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.
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.
uva 10048 Audiophobia_第1张图片
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
题目大意:
给你一个无向图,然后给你一堆查询。每个查询分别输入两个节点a,b,然后问你从a到b之间的所有路径中,找出一条路径使得这条路径里边权值最大的值最小。(比较绕口,仔细翻译下原题即可)
思路见代码下方

#include
#include
#include
#include
#include
#include
#include
#include
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;i1;//改动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//改动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<cout<<"Case #"<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"<else
            cout<return 0;
}











思路:
(说的好絮叨,为了能不让自己过两天忘了)
首先考虑,既然要求从a到b走的边必须选择权值最小的(注意不是权值的和),那么如果有a-c-b是10和5另外有一条边是a-d-b是2和3,那明显应该走a-d-b,由此看来,这是最小生成树的节奏(注意,这里有森林的情况,比如输入样例里的样例2),在加上要求任意两个节点的值可以考虑floyed的变形。
知道是最小生成树了,先选个算法,有prim和kruskal可选,我通常都是用kruskal算法。其实这道题用kruskal可以做,而且后来看大多数人代码都是用kruskal加floyed。但是我突然想到一个逼格更高的算法用prim加动态规划,类似于RMQ那种,具体思路如下。
首先选择prim算法,prim算法是随意找一个点作为起始点,然后找与其相接的节点里权值最小的那个边。
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就行。
如图
uva 10048 Audiophobia_第2张图片
2.
把每次找到的点塞到一个vector里,用动态规划每次更新当前找到的节点和上一次找到的节点(上次找到的节点肯定是找到的最小生成树的边)之间的权值去更新之前所有的边。状态转移方程为
首先把min先加入到区间中,dp[pos][pr.e]=dp[pr.e][pos]=min,表示节点pre和节点pos之前的权值是min。
然后用转移方程
dp[pos][vp[i]]=dp[vp[i]][pos]=max(dp[vp[i]][pre],dp[pos][pre])
vp中保存的全是之前找到的节点。
比如找到了节点2-1-3-6现在找到了节点4,用4和6之间的权值去更新4到3,4到1,4到2的大小

你可能感兴趣的:(贪心\模拟\STL\暴力,数据结构,动态规划)