博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lightoj1002——Country Roads(最短路变形)
阅读量:2343 次
发布时间:2019-05-10

本文共 2051 字,大约阅读时间需要 6 分钟。

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 are m 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 ≤ n ≤ 500) and m (0 ≤ m ≤ 16000). The next m lines, each will contain three integers u, v, w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 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

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
Output for Sample Input
1
Case 1:
4
0
3
7
7
Case 2:
4
0
3
Impossible
Impossible

从u到v点有多条路径,路径的花费是权值最长的那段路,而u到v的最短路径就是花费最小的那个。求给出的点到其他点的所有最短路径

设dis保存当前点的最短路,由于题目还是求最短路,所以本质上还是要每次找到个最小的dis,不同的是dis的求法。
另外还要注意有重边

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 25#define Mod 10001using namespace std;int map[1005][1005],vis[1005],dis[1005],n,m;void dijkstra(int s){ int i,j,mi,v; memset(vis,0,sizeof(vis)); for(i=0; i
w) { map[u][v]=w; map[v][u]=w; } } int s; scanf("%d",&s); dijkstra(s); printf("Case %d:\n",cas); for(int i=0; i

转载地址:http://lpcvb.baihongyu.com/

你可能感兴趣的文章
linux 安装mysql
查看>>
利用SQL语句查询数据库中所有表
查看>>
ActiveMQ 安装
查看>>
java可变参数
查看>>
spring 简述
查看>>
HttpClient-03Http状态管理
查看>>
spring cloud 启动报错-must be declared as an @AliasFor [serviceId], not [name].
查看>>
常用软件下载地址
查看>>
修改spring boot 启动logo
查看>>
spring boot-启动及配置文件
查看>>
HttpsURLConnection 安全传输(HTTPS--Secure Hypertext Transfer Protocol-安全超文本传输协议)...
查看>>
ASP.NET跨页面传值的技巧
查看>>
ASP.NET页面之间传递值解析
查看>>
我要学ASP.NET MVC 3.0(八): MVC 3.0 传递和保存你的Model
查看>>
我要学ASP.NET MVC 3.0(九): MVC 3.0 验证你的Model
查看>>
我要学ASP.NET MVC 3.0(十): MVC 3.0 使用 Forms身份验证
查看>>
我要学ASP.NET MVC 3.0(十一): MVC 3.0 使用筛选器
查看>>
ASP.NET MVC3、Pager 分页
查看>>
在 ASP.NET MVC 中创建自定义 HtmlHelper 控件
查看>>
MSDN---扩展方法 (C# 方法中的this参数)
查看>>