1002 - Country Roads

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

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

Note

Dataset is huge, user faster I/O methods.


PROBLEM SETTER: JANE ALAM JAN
思路:最小生成树变形。
由于要找一条路径从出发点到另一点的路径中最大边的值最小。
根据最小生成树的算法,我们在算最小生成树的时候优先选择到集合路径最小的点加入集合,并把边加上,因为我们要选的从出发点到另一点的路径中最大边的值最小。
所以由最小生成树的优先可以知道我们所选的边是当前最小的,,所以满足要保证路径上的边都是最优的,并且更新的时候为  d[vec[l][i].to]=max(vec[l][i].cost,d[l]);
这就保证了,到当前的点的路的最大值,这样每次选最大值最小的。
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<queue>
6 #include<vector>
7 #include<string.h>
8 void prim(int n,int cnt);
9 int ma[600][600];
10 using namespace std;
11 typedef long long LL;
12 typedef struct pp {
13 int from;
14 int to;
15 int cost;
16 } ss ;
17 int d[600];
18 vector<ss>vec[600];
19 int main(void) {
20 int i,j,k,p,q;
21 int x,y,z;
22 int s;
23 scanf("%d",&k);
24 for(s=1; s<=k; s++) {
25 for(i=0; i<600; i++) {
26 vec[i].clear();
27 for(j=0; j<600; j++) {
28 ma[i][j]=1e9;
29
30 }
31 }
32 scanf("%d %d",&p,&q);
33 while(q--) {
34 scanf("%d %d %d",&x,&y,&z);
35 if(ma[x][y]>z) {
36 ma[y][x]=z;ma[x][y]=z;
37 }
38 }
39 for(i=0; i<600; i++) {
40 for(j=0; j<600; j++) {
41 if(ma[i][j]!=1e9) {
42 ss dd;
43 dd.cost=ma[i][j];
44 dd.from=i;
45 dd.to=j;
46 vec[dd.from].push_back(dd);
47 }
48 }
49 }
50 int u;
51 cin>>u;
52 prim(u,p-1);
53 printf("Case %d:\n",s);
54 for(i=0; i<p; i++)
55 if(d[i]!=1e9)printf("%d\n",d[i]);
56 else printf("Impossible\n");
57 }
58 return 0;
59
60 }
61 void prim(int n,int cnt) {
62 fill(d,d+600,1e9);
63 d[n]=0;
64 queue<int>que;bool flag[600];
65 memset(flag,0,sizeof(flag));
66 while(true)
67 {
68 int l=-1;int i;
69 for(i=0;i<=cnt;i++)
70 {
71 if((l==-1||d[i]<d[l])&&!flag[i])
72 {
73 l=i;
74 }
75 }
76 if(l==-1||d[l]==1e9)
77 {
78 break;
79 }
80 flag[l]=true;
81 for(i=0;i<vec[l].size();i++)
82 {
83 if(d[vec[l][i].to]>vec[l][i].cost)
84 {
85 d[vec[l][i].to]=max(vec[l][i].cost,d[l]);
86 }
87 }
88 }
89 }

1002 - Country Roads(light oj)的更多相关文章

  1. Lightoj 1002 - Country Roads(prim算法)

    I am going to my home. There are many cities and many bi-directional roads between them. The cities ...

  2. Light oj 1002 Country Roads (Dijkstra)

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...

  3. (期望)A Dangerous Maze(Light OJ 1027)

    http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...

  4. 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...

  5. (状压) Brush (IV) (Light OJ 1018)

    http://www.lightoj.com/volume_showproblem.php?problem=1018   Mubashwir returned home from the contes ...

  6. (light OJ 1005) Rooks dp

    http://www.lightoj.com/volume_showproblem.php?problem=1005        PDF (English) Statistics Forum Tim ...

  7. (light oj 1306) Solutions to an Equation 扩展欧几里得算法

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...

  8. (light oj 1319) Monkey Tradition 中国剩余定理(CRT)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...

  9. (light oj 1024) Eid (最小公倍数)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1024 In a strange planet there are n races. ...

随机推荐

  1. HashMap有几种遍历方法?推荐使用哪种?

    本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...

  2. A Child's History of England.41

    When intelligence of this new affront [hit in the face, c-o-n-frontation!] was carried to the King i ...

  3. 【leetcode】721. Accounts Merge(账户合并)

    Given a list of accounts where each element accounts[i] is a list of strings, where the first elemen ...

  4. Apache架构师的30条设计原则

    本文作者叫 Srinath,是一位科学家,软件架构师,也是一名在分布式系统上工作的程序员. 他是 Apache Axis2 项目的联合创始人,也是 Apache Software 基金会的成员. 他是 ...

  5. vue中vuex的五个属性和基本用法

    VueX 是一个专门为 Vue.js 应用设计的状态管理构架,统一管理和维护各个vue组件的可变化状态(你可以理解成 vue 组件里的某些 data ). Vuex有五个核心概念: state, ge ...

  6. 【Python】数据处理分析,一些问题记录

    不用造轮子是真的好用啊 python中单引号双引号的区别 和cpp不一样,cpp单引号表示字符,双引号表示字符串,'c'就直接是ascii值了 Python中单引号和双引号都可以用来表示一个字符串 单 ...

  7. 【转】在本地运行leetcode核心代码

    https://zhuanlan.zhihu.com/p/342993772 在调用solution之前,要加一句 Solution solution; solution.函数名(输入变量); 以下是 ...

  8. redis迁移工具redis-migrate-tool

    目录 一.简介 二.测试 三.安装 四.验证 一.简介 redis-migrate-tool是在redis之间迁移数据的一个方便且有用的工具.他会已服务方式不断同步两边的数据.等到合适时间,中断red ...

  9. 使用 WPF 做个 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果

    本文是使用 WPF 做个 PowerPoint 系列的博客,本文来告诉大家如何解析 PPT 里面的文本描边效果,在 WPF 应用中绘制出来,实现像素级相同 背景知识 在开始之前,期望你了解了 PPT ...

  10. ciscn_2019_en_3

    例行检查我就不放了,64位的程序放入ida中 可以看到s到buf的距离是0x10,因为puts是遇到\x00截止.而且题目没有限制我们s输入的数量,所以可以通过这个puts泄露出libc的基值 很明显 ...