1002 - Country Roads(light oj)
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.
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)的更多相关文章
- Lightoj 1002 - Country Roads(prim算法)
I am going to my home. There are many cities and many bi-directional roads between them. The cities ...
- Light oj 1002 Country Roads (Dijkstra)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...
- (期望)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 ...
- 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...
- (状压) Brush (IV) (Light OJ 1018)
http://www.lightoj.com/volume_showproblem.php?problem=1018 Mubashwir returned home from the contes ...
- (light OJ 1005) Rooks dp
http://www.lightoj.com/volume_showproblem.php?problem=1005 PDF (English) Statistics Forum Tim ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- (light oj 1319) Monkey Tradition 中国剩余定理(CRT)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional ...
- (light oj 1024) Eid (最小公倍数)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1024 In a strange planet there are n races. ...
随机推荐
- A Child's History of England.51
CHAPTER 14 ENGLAND UNDER KING JOHN, CALLED LACKLAND At two-and-thirty years of age, John became King ...
- A Child's History of England.5
Above all, it was in the Roman time, and by means of Roman ships, that the Christian Religion was fi ...
- day09 orm查询优化相关
day09 orm查询优化相关 今日内容概要 orm字段相关补充 orm查询优化相关 orm事务操作 图书管理系统练习 今日内容详细 orm事务操作 """ 事务:ACI ...
- HTTP 之 options预请求
一.HTTP一共有八种常见请求方法 get:参数在url上,浏览器长度有限制,不安全 post:参数不可见,长度不受限制 put:上传最新内容到指定位置 delete:删除请求的url所表示的资源 h ...
- win32汇编基础
win32汇编基础知识 Debug 版本|Release 版本 Debug 是"调试"的意思,Debug 版本就是为调试而生的,编译器在生成 Debug 版本的程序时会加入调试辅助 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- Swift alert 倒计时
let title: String = "您的开奖时间为" let time: String = "2017-10-23 12:23:18" let count ...
- Java Maven项目搭建
创建空项目 New Project --> Empty Project --> ... 配置JDK Project Settings --> Project 选择JDK Module ...
- feign中开启熔断的书写步骤
/** 1.在pom.xml中引入依赖 2.在application.yaml中开启hystrix 3.在方法上配置熔断类 4.书写接口的实现类 **/ //1.在pom.xml中引 ...
- 连接opcserver时报错 connecting to OPC Server "****" CoCreateInstance 服务器运行失败
在普通windows系统连接OPCServer可能会报这样的错,排查很长时间,OPCServer跟Client都运行正常,点号录入也正常. 最后发现,其实是OPCServer 与OPCClient 权 ...