关闭

uva 10048 Audiophobia

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

#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;
}











思路:
(说的好絮叨,为了能不让自己过两天忘了)
首先考虑,既然要求从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就行。
如图
这里写图片描述
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的大小

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

例题11-5 噪音恐惧症 UVa10048

1.题目描述:点击打开链接 2.解题思路:本题的解题过程类似于Floyd算法的原理,即:任意一条长度大于1的路径,一定存在一个中间点k,使得d(i,j)=d(i,k)+d(k,j),其中d(i,j)表...
  • u014800748
  • u014800748
  • 2015-03-04 22:30
  • 588

UVa 10048 Audiophobia (任意两点最长边的最小化)

Kruskal算法: #include #include #include #include #include #include #include #include #include #includ...
  • Slow_Wakler
  • Slow_Wakler
  • 2015-08-05 09:47
  • 247

UVaOJ 10048 Audiophobia(最小化最短路的最大权值边)

Audiophobia Consider yourself lucky! Consider yourself lucky to be still breathing and having fun p...
  • qq_26658823
  • qq_26658823
  • 2016-09-06 15:41
  • 192

uva 10048 图论--最短路 flody算法

#include #include #include #include using namespace std; #define N 1050 int a[N][N]; int main() { i...
  • qq_24489717
  • qq_24489717
  • 2015-06-10 23:50
  • 353

UVA-815 flooded!(白书说能锻炼思维的题)

题意挺长的,而且是英文的,我这里就简述一下吧,就是有m*n个格子,每个格子都是10*10的规格(高度是无限的),给出每个格子的海拔,然后给出一个洪水的量,输出最后水深度和完全被水覆盖的面积百分比。这个...
  • FishSeeker
  • FishSeeker
  • 2016-04-08 09:36
  • 950

习题4-8 特别困的学生 UVa12108

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #define N 1000000 int n...
  • u014800748
  • u014800748
  • 2014-08-06 21:35
  • 1866

UVA 1658 - Admiral (拆点+最小费用流)

该题中的拆点法是解决几点容量的通用方法 。  因为只有容量限制的话仍然不能满足每个结点只访问一次这个限制 ,原因很简单,大家画个图就知道了,假设从起点有两条路到同一个结点2,然后又都到末点n,虽然它们...
  • weizhuwyzc000
  • weizhuwyzc000
  • 2015-08-21 20:53
  • 1093

uva 12174 - Shuffle(预处理+暴力)

题目链接:uva 12174 - Shuffle 题目大意:有一个播放器用于播放音乐,现在给出s(已有曲目的数量),n给出记录的长度。播放器有随机播放的功能,每次生成一个1~s的随机系列进行...
  • u011328934
  • u011328934
  • 2014-02-16 19:05
  • 1492

Rails(UVa514)(栈)

Rails  There is a famous railway station in PopPush City. Country there is incredibly hilly. Th...
  • hdd871532887
  • hdd871532887
  • 2015-12-21 07:13
  • 645

例题10-2 不爽的裁判 UVa12169

1.题目描述:点击打开链接 2.解题思路:本题要求找到合适的一组a,b,使得按照递推公式能输出正确的x2,x4...可以枚举a值,通过列写方程得到b的值,但这里有一个问题,这里是一个同余方程,等号的...
  • u014800748
  • u014800748
  • 2015-02-20 12:31
  • 887
    最新评论