LightOJ 1002 Country Roads(最短路变形(SPFA||贝尔曼))

Time Limit:3000MS    Memory Limit:32768KB     64bit IO Format:%lld & %llu

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) andm (0 ≤ m ≤ 16000). The next m lines, each will contain three integersu, 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 integert (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


类似 POJ2253.

题目大意: 计算从全部城市到T城市的最小花费的最大距离。


注意数组大小的问题,之前开的640的数组,并且结构体开的数组太小。应该至少开16000*2;因为是无向图。


法一:

贝尔曼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
int Map[1010][1010],dis[1010];
int n,m,t,s;
int ma(int a,int b)
{
    return a>b?a:b;
}
int mi(int a,int b)
{
    return a>b?b:a;
}
struct node
{
    int u,v,w;
}q[206000];
void BF()
{
    int i,j,k;
    for(i=0;i<n;i++)
        dis[i]=inf;
    dis[t]=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<s;j++)
        {
            dis[q[j].v ]=mi(ma(dis[q[j].u ],q[j].w),dis[q[j].v ] );
        }
    }
}
int main()
{
    int i,j,k,x,y,cla;
    scanf("%d",&cla);
    for(int zu=1;zu<=cla;zu++)
    {
        scanf("%d%d",&n,&m);
        s=0;
        int a,b,c;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            q[s].u=a;
            q[s].v=b;
            q[s++].w=c;
            q[s].v=a;
            q[s].u=b;
            q[s++].w=c;
        }
        scanf("%d",&t);
        printf("Case %d:\n",zu);
        BF();
        for(i=0;i<n;i++)
        {
            if(dis[i]<=20000)
                printf("%d\n",dis[i]);
            else
                puts("Impossible");
        }
    }
    return 0;
}



法二:

SPFA:



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAX 16000
#define inf 0x3f3f3f3f
using namespace std;

int n,m,s;
struct ndoe
{
    int v,w,next;
} Edge[16000*5];

int Dis[1010];
int Head[666],t;
bool vis[666];//SPFA中有判断元素是否进栈。

void Creat(int u,int v,int w)
{
    Edge[s].v=v;
    Edge[s].w=w;
    Edge[s].next=Head[u];
    Head[u]=s++;
}

int Ma(int a,int b)
{
    return a>b?a:b;
}

void SPFA()
{
    queue<int >q;
    int i,j,k,x;
    while(!q.empty())
        q.pop();
    memset(Dis,inf,sizeof(Dis));//可直接对Dis[]进行改变值
    memset(vis,false,sizeof(vis));
    vis[t]=true;
    Dis[t]=0;
    q.push(t);
    while(!q.empty())
    {
        k=q.front();
        x=Head[k];//首先是从根节点拿出值,并依次取出根下的值
        q.pop();
        vis[k]=false;//清标记
        while(x!=-1)//不断的延根取值,直道底
        {
            if(Dis[Edge[x].v]>Ma(Edge[x].w,Dis[k] ) )
            {
                Dis[Edge[x].v]=Ma(Edge[x].w,Dis[k] );
                if(!vis[ Edge[x].v ])
                {
                    q.push(Edge[x].v);
                    vis[Edge[x].v ]=true;
                }
            }
            x=Edge[x].next;//继续向下延伸。
        }
    }

}

int main()
{
    int Cla;
    scanf("%d",&Cla);
    for(int Group=1; Group<=Cla; Group++)
    {
        memset(Head,-1,sizeof(Head));
        scanf("%d%d",&n,&m);
        s=0;
        for(int i=0; i<m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            Creat(a,b,c);
            Creat(b,a,c);
        }
        scanf("%d",&t);
        SPFA();
        printf("Case %d:\n",Group);
        for(int i=0; i<n; i++)
        {
            if(Dis[i]<=20000)
            {
                printf("%d\n",Dis[i]);
            }
            else
                puts("Impossible");
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值