Lightoj 1002 - Country Roads(prim算法)
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算法)的更多相关文章
- 1002 - Country Roads(light oj)
1002 - Country Roads I am going to my home. There are many cities and many bi-directional roads betw ...
- hdu-1102-Constructing Roads(Prim算法模板)
题目链接 /* Name:hdu-1102-Constructing Roads Copyright: Author: Date: 2018/4/18 9:35:08 Description: pr ...
- Light oj 1002 Country Roads (Dijkstra)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1002 题目描述: 有n个城市,从0到n-1开始编号,n个城市之间有m条边,中 ...
- Light oj-1002 - Country Roads,迪杰斯特拉变形,不错不错~~
1002 - Co ...
- 最小生成树问题------------Prim算法(TjuOj_1924_Jungle Roads)
遇到一道题,简单说就是找一个图的最小生成树,大概有两种常用的算法:Prim算法和Kruskal算法.这里先介绍Prim.随后贴出1924的算法实现代码. Prim算法 1.概览 普里姆算法(Prim算 ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- Jungle Roads_hdu_1301(prim算法)
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- hdu 3371(prim算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
随机推荐
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- js正则替换十六进制
var re=/\x62/;//没有0,也没有分号。alert(re.test("blue")); //output "true" 需要使用< 如需显示 ...
- 大数据学习——有两个海量日志文件存储在hdfs
有两个海量日志文件存储在hdfs上, 其中登陆日志格式:user,ip,time,oper(枚举值:1为上线,2为下线):访问之日格式为:ip,time,url,假设登陆日志中上下线信息完整,切同一上 ...
- XTUOJ 15503 - C
15503 - C Accepted: 6 Submissions: 27 Time Limit: 3000 ms Memory Limit: 1048576 KB 在解决了小女孩的 ...
- Halloween Costumes(区间DP)
Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning t ...
- [NOIP1998] 普及组
三连击 题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例,试求出所有满足条件的三个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行, ...
- 【BZOJ2038】小Z的袜子(莫队)
题意: 给定n个数a1, a2…… an与m个询问(L,R).对于每个询问,从aL, aL+1…… aR这R-L+1个数中随机取出两个数,求这两个数相同的概率. 数据范围:1<=n,m,ai&l ...
- Eval 和 Bind 的区别
原文发布时间为:2008-10-20 -- 来源于本人的百度文章 [由搬家工具导入] 据绑定表达式包含在 <%# 和 %> 分隔符之内,并使用 Eval 和 Bind 函数。 Eval 函 ...
- Python()-类命名空间和对象/实例命名空间
类命名空间和对象/实例命名空间: 创建类, 就会创建一个类的名称空间, 空间:存储类的属性 属性: 静态属性:直接定义在类下面 & 和类名关联 的变量 对象属性:在类内和self关联 & ...
- 为什么zookeeper的节点配置的个数必须是奇数个?
zookeeper有这样一个特性:集群中只要有过半的机器是正常工作的,那么整个集群对外就是可用的.也就是说如果有2个zookeeper,那么只要有1个死了zookeeper就不能用了,因为1没有过半, ...