LightOJ - 1002(最短路变形+邻接表)

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

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

Note

Dataset is huge, user faster I/O methods.


题意:求出每段路中最大值的最小值。

思路:用最短路算法变形即可。

以下为出 Bellman-Ford算法和 Dijkstra算法两种求法, Dijkstra算法速度要比 Bellman-Ford快,不过 Bellman-Ford更加简单且能算负环。
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
    int x,y,s;
} a[16010*2];
int dis[510];
int t,n,m,k,qi;
void FB(int qi)
{
    memset(dis,0x3f,sizeof(dis));
    dis[qi]=0;
    for(int i=0; i<n; i++)
    {
        for(int i=0; i<k; i++)
        {
            dis[a[i].y]=min(max(dis[a[i].x],a[i].s),dis[a[i].y]);
        }

    }
}
int main()
{
    int ph=1;
    scanf("%d",&t);
    while(t--)
    {
        k=0;
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&a[k].x,&a[k].y,&a[k].s);
            a[k+1].x=a[k].y;
            a[k+1].y=a[k].x;
            a[k+1].s=a[k].s;
            k+=2;
        }
        scanf("%d",&qi);
        FB(qi);
        printf("Case %d:\n",ph++);
        for(int i=0; i<n; i++)
        {
            if(dis[i]>=0x3f3f3f)
            {
                printf("Impossible\n");
                continue;
            }
            printf("%d\n",dis[i]);
        }
    }
}

Dijkstra+邻接表:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define M 16010
#define inf 0x3f3f3f
using namespace std;
int t,n,m,k,qi;
int u[M*2],v[M*2],w[M*2];
int first[510],next[M*2];
int dis[M*2],vis[M*2];
void djskl(int qi)
{
    int pan,k,flag,l;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[qi]=0;
    for(int i=0; i<n; i++)
    {
        pan=inf;
        flag=0;
        for(int j=0; j<n; j++)
        {
            if(dis[j]<pan&&!vis[j])
            {
                k=j;
                pan=dis[j];
                flag=1;
            }
        }
        if(!flag)
            break;
        vis[k]=1;
        l=first[k];
        while(l!=-1)
        {
            dis[v[l]]=min(max(dis[u[l]],w[l]),dis[v[l]]);
            l=next[l];
        }
    }
}
int main()
{
    int ph=1;
    scanf("%d",&t);
    while(t--)
    {
        k=0;
        memset(first,-1,sizeof(first));
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u[k],&v[k],&w[k]);
            next[k]=first[u[k]];//邻接表核心
            first[u[k]]=k;//邻接表核心
            v[k+1]=u[k];
            u[k+1]=v[k];
            w[k+1]=w[k];
            next[k+1]=first[u[k+1]];
            first[u[k+1]]=k+1;
            k+=2;
        }
        scanf("%d",&qi);
        djskl(qi);
        printf("Case %d:\n",ph++);
        for(int i=0; i<n; i++)
        {
            if(dis[i]>=inf)
            {
                printf("Impossible\n");
                continue;
            }
            printf("%d\n",dis[i]);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值