F题【LightOJ 1002】【dijkstra】

来源:互联网 发布:fifaol3非i发数据库 编辑:程序博客网 时间:2024/06/06 09:11

Description

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from0 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 betweenu 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'.


题意:总共有T组数据,每组数据输入n和m分别代表n个点和m条边,最后输入源点t,求


每一点到源点的最短路径。最短路径:该条路上的最长距离。


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

Sample Output

Case 1:

4

0

3

7

7

Case 2:

4

0

3

Impossible

Impossible

<span style="font-size:24px;">#include <cstdio>#include <vector>#include <queue>#include <cstring>#define MAX 10000#define INF 0x3f3f3f3fusing namespace std;struct dij                               //封装dijkstra算法{    struct edge                       //定义边的结构体    {        int from,to,dist;        edge() {}        edge(int a,int b,int c):from(a),to(b),dist(c) {}    };    struct info                         //定义含有边和该边终点的结构体    {        int d,u;        info() {}        info(int a,int b):d(a),u(b) {}        bool operator < (const info aa)const    //重载运算符,当此结构体放在优先队列里时,排序按该重载运算符排序        {            return d>aa.d;        }    };    int n,m;    vector<edge>edges;         //边集合    vector<int>s[MAX];           //邻接表存图    bool vis[MAX];     int d[MAX];    void init(int n)                   //初始化,清空邻接表和边集合    {        this->n=n;        for(int i=0; i<=n; i++)            s[i].clear();        edges.clear();    }    void add(int from,int to,int dist)     //建图    {        edges.push_back(edge(from,to,dist));        m=edges.size();        s[from].push_back(m-1);    }    void solve(int ss)           //求最短路    {        priority_queue<info>q;        q.push(info(0,ss));        //先将源点放入队列中        for(int i=0; i<=n; i++)                 d[i]=INF;        d[ss]=0;        memset(vis,0,sizeof(vis));        while(!q.empty())        {            info tt=q.top();            q.pop();            int u=tt.u;            if(vis[u])                continue;            vis[u]=true;            int len=s[u].size();            for(int i=0; i<len; i++)            {                edge e=edges[s[u][i]];                int x=max(d[u],e.dist);                if(d[e.to]>x)                {                    d[e.to]=x;                    q.push(info(d[e.to],e.to));                }            }        }    }} D;int main(){    //freopen("abc.text","r",stdin);    int T,n,m,u,v,w,t;    scanf("%d",&T);    for(int k=1; k<=T; k++)    {        scanf("%d %d",&n,&m);        D.init(n);        for(int i=0; i<m; i++)        {            scanf("%d %d %d",&u,&v,&w);            D.add(u,v,w);                                 //这是无向图            D.add(v,u,w);        }        scanf("%d",&t);        D.solve(t);        for(int i=0; i<n; i++)        {            if(D.d[i]>=INF)                printf("Impossible\n");            else                printf("%d\n",D.d[i]);        }    }}</span><span style="font-weight: bold; font-size: 11pt;"></span>


0 0