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. 零基础学习java------34---------登录案例,域,jsp(不太懂),查询商品列表案例(jstl标签)

    一. 简单登录案例 流程图: 项目结构图 前端代码: <!DOCTYPE html> <html> <head> <meta charset="UT ...

  2. CAS你知道吗

    1.比较并交换 CASDemo /** * CAS => compareAndSet * 比较并交换 */ public class CASDemo { public static void m ...

  3. APP调用系统相册,使用3DTouch重压,崩溃

    崩溃:app调用系统相册,使用3DTouch重压,崩溃 问题描述 app调用系统相册,使用3DTouch重压,一般的app都会崩溃. 解决方法 写个分类即可 @implementation UICol ...

  4. OC-私有方法,构造方法,类的本质及启动过程

    总结 标号 主题 内容 一 OC的私有方法 私有变量/私有方法 二 @property 概念/基本使用/寻找方法的过程/查找顺序 三 @synthesize @synthesize概念/基本使用/注意 ...

  5. 使用Spring Data ElasticSearch框架来处理索引

    /**步骤:创建工程,导入相应的包--->配置文件---->创建实体类对象------>创建接口---->测试增删改查的方法 **/ //步骤:创建工程,导入相应的包 < ...

  6. ActiveMQ(三)——理解和掌握JMS(1)

    一.JMS基本概念 JMS是什么JMS Java Message Service,Java消息服务,是JavaEE中的一个技术. JMS规范JMS定义了Java中访问消息中间件的接囗,并没有给予实现, ...

  7. 【JS】原生实现拖拽

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  8. Declarative Pipeline 基础语法

    Declarative Pipeline(声明式)核心概念 核心概念用来组织pipeline的运行流程 1.pipeline :声明其内容为一个声明式的pipeline脚本 2.agent:执行节点( ...

  9. 熔断和降级的初步详解实现(NET Core控制台输出讲解Polly)

    概述 很多朋友包括我,对于"八股文"可以说是比较熟练的,每次面试前都会专研不少东西,各种固定答案.专业术语都是张口就来,一个字,稳. 八股文:程序员八股文是指程序员在面试过程中经常 ...

  10. 安装xampp开发环境更改默认项目路径

    xampp开发环境中默认的项目路径在xampp下的htdocs文件下 如果想修改默认项目的位置步骤如下: 1)D:\xampp\apache\conf 找到httpd.conf打开 2)找到 Docu ...