随笔 - 455  文章 - 9  评论 - 17  阅读 - 18万

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 << 61;
const double eps=1e-5;
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[510][510];
int w[510][510];
int vis[510],lowcost[510];
int n,m,k;
int dis[maxn];
void prime(){
    memset(dis,-1,sizeof dis);
    dis[k]=0;
    cle(vis);
    vis[k]=1;
    for(int i=0;i<n;i++){
        lowcost[i]=edge[k][i];
    }
    int Min,x;
    while(1){
        Min=INF;
        for(int i=0;i<n;i++){
            if(lowcost[i]<Min&&!vis[i]){
                Min=lowcost[i],x=i;
            }
        }
        if(Min==INF)break;
        vis[x]=1;
        dis[x]=Min;
        for(int i=0;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=1;t<=T;t++){
        printf("Case %d:\n",t);
        memset(edge,INF,sizeof edge);
        cin>>n>>m;
        for(int i=1;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=0;i<n;i++){
            if(dis[i]==-1)puts("Impossible");
            else printf("%d\n",dis[i]);
        }
    }
    return 0;
}
复制代码

 

posted on   Beserious  阅读(310)  评论(0编辑  收藏  举报
编辑推荐:
· 时间轮在 Netty , Kafka 中的设计与实现
· MySQL 优化利器 SHOW PROFILE 的实现原理
· 在.NET Core中使用异步多线程高效率的处理大量数据
· 聊一聊 C#前台线程 如何阻塞程序退出
· 几种数据库优化技巧
阅读排行:
· 跟着 8.6k Star 的开源数据库,搞 RAG!
· 夜莺 v8 第一个版本来了,开始做有意思的功能了
· 3款.NET开源、功能强大的通讯调试工具,效率提升利器!
· 推荐一个C#轻量级矢量图形库
· .NET 9 增强 OpenAPI 规范,不再内置swagger
< 2024年12月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4
5 6 7 8 9 10 11

点击右上角即可分享
微信分享提示