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.

/* ***********************************************
Author :guanjun
Created Time :2016/6/6 18:42:59
File Name :1002.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
int edge[][];
int w[][];
int vis[],lowcost[];
int n,m,k;
int dis[maxn];
void prime(){
memset(dis,-,sizeof dis);
dis[k]=;
cle(vis);
vis[k]=;
for(int i=;i<n;i++){
lowcost[i]=edge[k][i];
}
int Min,x;
while(){
Min=INF;
for(int i=;i<n;i++){
if(lowcost[i]<Min&&!vis[i]){
Min=lowcost[i],x=i;
}
}
if(Min==INF)break;
vis[x]=;
dis[x]=Min;
for(int i=;i<n;i++){
if(!vis[i]&&max(dis[x],edge[x][i])<lowcost[i]){
lowcost[i]=max(edge[x][i],dis[x]);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int T,x,y,z;
cin>>T;
for(int t=;t<=T;t++){
printf("Case %d:\n",t);
memset(edge,INF,sizeof edge);
cin>>n>>m;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
if(z<edge[x][y]){
edge[x][y]=z;
edge[y][x]=z;
}
}
cin>>k;
prime();
for(int i=;i<n;i++){
if(dis[i]==-)puts("Impossible");
else printf("%d\n",dis[i]);
}
}
return ;
}

Lightoj 1002 - Country Roads(prim算法)的更多相关文章

  1. 1002 - Country Roads(light oj)

    1002 - Country Roads I am going to my home. There are many cities and many bi-directional roads betw ...

  2. hdu-1102-Constructing Roads(Prim算法模板)

     题目链接 /* Name:hdu-1102-Constructing Roads Copyright: Author: Date: 2018/4/18 9:35:08 Description: pr ...

  3. Light oj 1002 Country Roads (Dijkstra)

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

  4. Light oj-1002 - Country Roads,迪杰斯特拉变形,不错不错~~

                                                                                               1002 - Co ...

  5. 最小生成树问题------------Prim算法(TjuOj_1924_Jungle Roads)

    遇到一道题,简单说就是找一个图的最小生成树,大概有两种常用的算法:Prim算法和Kruskal算法.这里先介绍Prim.随后贴出1924的算法实现代码. Prim算法 1.概览 普里姆算法(Prim算 ...

  6. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  7. Jungle Roads_hdu_1301(prim算法)

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. hdu 3371(prim算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...

  9. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

随机推荐

  1. Leetcode 222.完全二叉树的节点个数

    完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最 ...

  2. Cow Exhibition (01背包)

    "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...

  3. [luoguP1273] 有线电视网(DP)

    传送门 f[i][j]表示节点i选j个用户的最大收益 #include <cstdio> #include <cstring> #include <iostream> ...

  4. hdu 4539

    #include<stdio.h> #include<string.h> ]; int s]; int main() { int i,j,n,m; int ch; while( ...

  5. BZOJ1700: [Usaco2007 Jan]Problem Solving 解题

    每月m<=1000块钱,有n<=300道题,要按顺序做,每月做题要花钱,花钱要第一个月预付下个月立即再付一次,给出预付和再付求最少几个月做完题,第一个月不做. 神奇的DP..竟没想出来.. ...

  6. Linux 常用但较容易忘记的命令

    看死循环 strace -p pid 查看系统版本 cat /etc/issue 设置内核启动版本 /etc/lilo.conf , /boot/grub/grub.conf 设置启动模式  /etc ...

  7. Spring Data Redis配置项有多少(不列举具体,只提供找的方法)

    首先,要说明Spring Data Redis集成了很多款客户端,比如Jedis这些. 而如果在注入Bean时,我们一般是可以设置一些项的,比如hostName和port等,对于这些项一般的查找方式通 ...

  8. Spring Boot中验证码实现kaptcha

    要生成验证码网上的方案比较多,基本是基于两大类:1为自定义生成,操作用Image类,2为kaptcha生成,有模糊算法. 当然也可以直接交由前端进行处理 1.基于kaptcha 首先不要怀疑的是报名是 ...

  9. Design Pattern Visitor 訪问者设计模式

    訪问者设计模式是已经有了一组Person对象了,然后不同的訪问者訪问这组对象.会有不同效果. 这些訪问者实际上就是一个能够让Person对象组运行的动作行为等. 至于这些Person对象是怎样运行这些 ...

  10. 【Nginx】Nginx的配置

    配置文件为.conf文件 一.块配置项 块配置项由一个块配置项名和一对大括号组成.具体如下: events{ ... } http{ upstream backend{ server 127.0.0. ...