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. ...
随机推荐
- PHP socket Workerman实用的php框架
PHP socket Workerman是一款开源高性能异步PHP socket即时通讯框架. 非常好用的一款框架,可以支持在线聊天,长连接等 用法 官方 https://www.workerman. ...
- LATEX公式语法
see how any formula was written in any question or answer, including this one, right-click on the ex ...
- 大数据学习----day27----hive02------1. 分桶表以及分桶抽样查询 2. 导出数据 3.Hive数据类型 4 逐行运算查询基本语法(group by用法,原理补充) 5.case when(练习题,多表关联)6 排序
1. 分桶表以及分桶抽样查询 1.1 分桶表 对Hive(Inceptor)表分桶可以将表中记录按分桶键(某个字段对应的的值)的哈希值分散进多个文件中,这些小文件称为桶. 如要按照name属性分为3个 ...
- centos7 自动同步时间
rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime vim /etc/sysconfig/cloc ...
- Activity 详解
1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...
- spring mvc idea创建
创建项目 创建项目 --> Spring --> Spring MVC --> 下面选择Download,会显示Spring MVC-5版本 如果是首次使用IDEA,因为没有配置ma ...
- Spring.DM版HelloWorld
本文主要描述使用Spring.DM2.0,创建OSGi的HelloWorld演示程序,理解Spring.DM的OSGi框架实现机制. 环境描述: 项目 版本 Eclipse 3.7.x JDK 1 ...
- maven根据profile,resources,filters来区分部署环境
项目过程中,在不同的阶段,分别需要部署开发环境,测试环境,线上环境.如果都用一套配置文件,很容易弄乱,所以维持多套配置文件很有必要. maven提供了一组属性以供开发人员灵活搭配,可以根据环境来打包, ...
- ubuntu qq/微信
Ubuntu qq&微信安装/启动脚本. Docker 本脚本依赖Docker,需要提前安装好Docker环境.参考https://yeasy.gitbooks.io/docker_pract ...
- DevOps到底是什么意思?
目录 一.方法论 二.DevOps的起源 三.DevOps到底是什么 四.DevOps的发展现状 五.DevOps与虚拟化.容器.微服务 一.方法论 提到DevOps这个词,我相信很多人一定不会陌生. ...