lightoj1002

来源:互联网 发布:hr 知乎 编辑:程序博客网 时间:2024/05/21 18:25

链接:http://lightoj.com/volume_showproblem.php?problem=1002

002 - Country Roads

PDF (English)

Statistics

Forum

 

Time Limit: 3 second(s)

Memory Limit: 32 MB

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There arem roads. You are given the number of my city t where I belong. Now from each city you have to find the minimum cost to go to my city. The cost is defined by the cost of the maximum road you have used to go to my city.

For example, in the above picture, if we want to go from 0 to 4, then we can choose

1)      0 - 1 - 4 which costs 8, as 8 (1 - 4) is the maximum road we used

2)      0 - 2 - 4 which costs 9, as 9 (0 - 2) is the maximum road we used

3)      0 - 3 - 4 which costs 7, as 7 (3 - 4) is the maximum road we used

So, our result is 7, as we can use 0 - 3 - 4.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a blank line and two integers n (1 ≤ ≤ 500) and m (0 ≤ ≤ 16000). The next m lines, each will contain three integers u, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ ≤ 20000) indicating that there is a road between u and v with cost w. Then there will be a single integer t (0 ≤ t < n). There can be multiple roads between two cities.

Output

For each case, print the case number first. Then for all the cities (from 0 to n-1) you have to print the cost. If there is no such path, print 'Impossible'.

Sample Input

Output for Sample Input

2

 

5 6

0 1 5

0 1 4

2 1 3

3 0 7

3 4 6

3 1 8

1

 

5 4

0 1 5

0 1 4

2 1 3

3 4 7

1

Case 1:

4

0

3

7

7

Case 2:

4

0

3

Impossible

Impossible

题目不解释了

代码:

//最近不知道为什么,套模板总是错,烦死了,检查半天只好重打,但还是不知道错误在哪

//此题与最短路不同,此题求得是到达t的所有路径中花费最多的金币的那条,而消费是由经过所有路的最大金币数决定的

//所以只需将map[mid][i]+dis[mid]<dis[i]改成max(map[mid][i],dis[mid])<dis[i]即可

#include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<cmath>using namespace std;const int maxn=505;const int inf=30000;int n,m,t;int map[maxn][maxn];int dis[maxn];bool used[maxn];void spfa(){     for(int i=0;i<n;i++)    {        dis[i]=inf;    }    dis[t]=0;    queue <int> q;    q.push(t);    used[t]=1;    while(!q.empty())    {        int mid=q.front();        q.pop();        used[mid]=0;        for(int i=0;i<n;i++)        {           if(max(map[mid][i],dis[mid])<dis[i])           {               dis[i]=max(map[mid][i],dis[mid]);               if(!used[i])               {                   q.push(i);                   used[i]=1;               }           }        }    }}int main(){    freopen("in.txt","r",stdin);    int T;    cin>>T;    for(int i=1;i<=T;i++)    {        cin>>n>>m;        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)            map[i][j]=(i==j?0:inf);        memset(used,0,sizeof(used));        for(int j=0;j<m;j++)        {            int u,v,w;            cin>>u>>v>>w;            if(w<map[u][v])  map[u][v]=map[v][u]=w;        }        cin>>t;        spfa();        cout<<"Case "<<i<<":"<<endl;        for(int i=0;i<n;i++)            if(dis[i]!=inf) cout<<dis[i]<<endl;            else cout<<"Impossible"<<endl;    }    return 0;}


0 0