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) 和 ...
随机推荐
- centos7.4下搭建JDK+Tomcat+Nginx+Mysql+redis+Mongodb+maven+Git+Jenkins
先干两件大事!先干两件大事!先干两件大事! 1.关闭selinux [root@mycentos ~]# vi /etc/selinux/config SELINUX=disabled 2.关闭防火墙 ...
- 【Ajax 3】JavaScript封装Ajax
导读:上一篇博客简单介绍了一下对Ajax中的核心对象XMLHttpRequest的封装.那么为了方便对Ajax的应用,我们还需要进一步的对Ajax的基本功能进行下一步的封装,不得不说的是早就有人专门写 ...
- SA模板
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; ; char ...
- 【PD】PowerDesigner生成数据字典
1.首先说明我使用的环境 --------------------------------第一种:不按模板导出导出数据字典----------------------------- 2.打开PDM模型 ...
- java容器详解(以Array Arrays ArrayList为例)
//先记录一个想法……java实在是太臃肿了,纯面向对象也有不少弊端…… //能不能把java精简一下啊! 先上结论: Array:认真看api索引的话,Array有两个.一个是sql中的接口,一个是 ...
- 【微信小程序】开发实战 之 「开发框架MINA构成」
小程序开发框架的目标是通过尽可能简单.高效的方式让开发者可以在微信中开发具有原生 APP 体验的服务. 微信团队为小程序提供的框架命名为MINA.MINA框架通过封装微信客户端提供的文件系统.网络通信 ...
- P1427 小鱼的数字游戏 洛谷
https://www.luogu.org/problem/show?pid=1427 题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字 ...
- crontab使用简介
crontab的配置文件: 前四行是用来配置crond任务运行的环境变量 第一行SHELL变量指定了系统要使用哪个shell,这里是bash 第二行PATH变量指定了系统执行命令的路径 第三行MAIL ...
- Enhance Magento 404 page
Magento default installation already has a predefined custom 404 page (no-route). But is it enough t ...
- Meteor表单
在本教程中,我们将告诉你如何使用 Meteor 的表单. 文本输入 首先,我们将创建一个 form 元素中文本输入字段和提交按钮. meteorApp/import/ui/meteorApp.html ...