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

1

Sample Output

Case 1:

4

0

3

7

7

Case 2:

4

0

3

Impossible

Impossible

最短路SPFA另用;

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#pragma comment(linker,"/STACK:102400000")
#define WW freopen("output.txt","w",stdout) const int Max = 160000; struct node
{
int v;
int w;
int next;
}Edge[Max*10];
int Head[550];
int n,m;
int top;
int t;
int Dis[550];
bool vis[550];
void Build(int u,int v,int w)
{
Edge[top].v=v;
Edge[top].w=w;
Edge[top].next=Head[u];
Head[u]=top;
top++;
}
void SPFA()
{
int a,b;
memset(Dis,INF,sizeof(Dis));
memset(vis,false,sizeof(vis));
Dis[t]=0;
vis[t]=true;
queue<int>Q;
Q.push(t);
while(!Q.empty())
{
a=Q.front();
Q.pop();
b=Head[a];
while(b!=-1)
{
if(Dis[Edge[b].v]>max(Dis[a],Edge[b].w))
{
Dis[Edge[b].v]=max(Dis[a],Edge[b].w);
if(!vis[Edge[b].v])
{
vis[Edge[b].v]=true;
Q.push(Edge[b].v);
}
}
b=Edge[b].next;
}
vis[a]=false;
} }
int main()
{
int T;
int u,v,w;
int W=1;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(Head,-1,sizeof(Head));
top=0;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
Build(u,v,w);
Build(v,u,w);
}
scanf("%d",&t);
SPFA();
printf("Case %d:\n",W++);
for(int i=0;i<n;i++)
{
if(Dis[i]<INF)
{
printf("%d\n",Dis[i]);
}
else
{
printf("Impossible\n");
}
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

LightOJ1002 分类: 比赛 最短路 2015-08-08 15:57 15人阅读 评论(0) 收藏的更多相关文章

  1. hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏

    a problem where OO seems more natural to me, implementing a utility class not instantiable. how to p ...

  2. ubuntu文件管理常用命令 分类: linux ubuntu 学习笔记 2015-07-02 16:57 29人阅读 评论(0) 收藏

    1.关闭防火墙:ufw disable 2.以.开头的表示隐藏文件 3..和..分别代表当前目录以及当前目录的父目录 4.显示当前用户所在目录pwd 5.touch创建空文件 6.mkdir创建新目录 ...

  3. winform 窗体最大化 分类: WinForm 2014-07-17 15:57 215人阅读 评论(0) 收藏

    1:窗体首次加载时最大化 (1):主窗体 this.WindowState = FormWindowState.Maximized; //窗体显示中间部分,不显示窗体名称和最小化.最大化.关闭按钮   ...

  4. DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏

    DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏

    DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 周赛-Equidistant String 分类: 比赛 2015-08-08 15:44 6人阅读 评论(0) 收藏

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. 周赛-Toy Cars 分类: 比赛 2015-08-08 15:41 5人阅读 评论(0) 收藏

    Toy Cars time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  8. C# 字符串常用操作 分类: C# 2014-08-22 15:07 238人阅读 评论(0) 收藏

    string str1 = "C#操作字符串<几种常见方式>如下"; string str2 = "C#操作字符串";     //比较字符串 Co ...

  9. 安装hadoop1.2.1集群环境 分类: A1_HADOOP 2014-08-29 15:49 1444人阅读 评论(0) 收藏

    一.规划 (一)硬件资源 10.171.29.191 master 10.173.54.84  slave1 10.171.114.223 slave2 (二)基本资料 用户:  jediael 目录 ...

  10. Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏

    1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...

随机推荐

  1. 使用Universal USB Installer安装Ubuntu

    1.下载Universal USB Installer 下载地址: 2.下载ubuntu 14 desktop.iso 运行Universal USB Installer,找到电脑上 ubuntu 1 ...

  2. tornado中使用Mako模版

    tornado是一个优秀的python的开源web 框架,框架本身的性能确实很好,但是他自带的模版只能说是一般般.关于tornado的详细信息可以直接到管网参考. http://www.tornado ...

  3. Java异步IO/NIO

  4. 学习OpenCV——鼠标事件(画框)

    #include "cv.h" #include "highgui.h" bool check_line_state=false; IplImage* work ...

  5. 如果类的属性是copy的NSString类型 用set方法拦截时

    @property (nonatomic, copy) NSString *badgeValue; - (void)setBadgeValue:(NSString *)badgeValue { _ba ...

  6. JDK安装 配置环境变量

    我将JDK安装在D盘中 在D盘中新建一个文件 文件名为JAVA 运行jdk安装软件 更改jdk安装路径为 下一步 等待jdk安装完成 安装jre路径 jre路径改为 点击下一步 等待jre安装完成 注 ...

  7. 转:Beautiful Soup

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...

  8. Object-C 入门

    该文章转载自:http://sheng.iteye.com/blog/775588一:Objective-C入门 .Cocoa的组成 苹果公司将Cocoa.Carbon.QuickTime和OpenG ...

  9. 反射认识_03_改变成员变量Fields

    包01:package ReflectionChange; public class ReflectionPoint_AB { String str1="access"; Stri ...

  10. PHP的几个常用加密函数(转载 https://jellybool.com/post/php-encrypt-functions)

    PHP的几个常用加密函数 在网站的开发过程中,常常需要对部分数据(如用户密码)进行加密,本文主要介绍PHP的几个常见的加密函数 MD5加密: string md5 ( string $str [, b ...