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. ...
随机推荐
- 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)
1.File 1.1 构造方法(只是创建已经存在文件的对象,并不能创建没有的文件) (1)public File(String pathname) (2)public File(String pare ...
- Gradle—Android配置详解
参考[1]彻底弄明白Gradle相关配置 [2]Android Studio gradle配置详解
- [学习总结]6、Android异步消息处理机制完全解析,带你从源码的角度彻底理解
开始进入正题,我们都知道,Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃.相信大家在日常的工作当中都会经常遇到这个问题,解决的方案应该也是早已烂熟于心,即创建一 ...
- zabbix之监控 io
#:编写监控脚本 root@ubuntu:/etc/zabbix/zabbix_agentd.conf.d# vim iotop_total.sh #!/bin/bash #Date: 2016/11 ...
- Initialization of data members
In C++, class variables are initialized in the same order as they appear in the class declaration. C ...
- Git命令行演练-团队开发
** 团队开发必须有一个共享库,这样成员之间才可以进行协作开发** ### 0. 共享库分类 > 本地共享库(只能在本地面对面操作) - 电脑文件夹/U盘/移动硬盘 & ...
- layui 弹窗中 分页展示table
1. 需求:点击查看更多,展示该类别 所有数据,并分页 2. 参考文档: (1)https://www.jianshu.com/p/40da11ebae66 (2) https://blog.csdn ...
- awk统计命令(求和、求平均、求最大值、求最小值)
本节内容:awk统计命令 1.求和 cat data|awk '{sum+=$1} END {print "Sum = ", sum}' 2.求平均 cat data|awk '{ ...
- Dubbo中CompletableFuture异步调用
使用Future实现异步调用,对于无需获取返回值的操作来说不存在问题,但消费者若需要获取到最终的异步执行结果,则会出现问题:消费者在使用Future的get()方法获取返回值时被阻塞.为了解决这个问题 ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(7. 安装Docker私有镜像仓库)
1. 简介 1.1. 自己做个私有镜像,方便上传和下载,我也在docker官网注册了一个账号,做好的镜像可以传上去 1.2. Redhat自带私有镜像的功能,需要安装包,这是howto: https: ...