POJ 3767 I Wanna Go Home【最短路floyd】

I Wanna Go Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3371 Accepted: 1445

Description

The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.

"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."

Would you please tell Mr. M at least how long will it take to reach his sweet home?

Input

The input contains multiple test cases.

The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.

The second line contains one integer M (0<=M<=10000), which is the number of roads.

The following M lines are the information of the roads. Each line contains three integers AB and T, which means the road between city A and city B will cost time TT is in the range of [1,500].

Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.

To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.

Note that all roads are bidirectional and there is at most 1 road between two cities.

Input is ended with a case of N=0.

Output

For each test case, output one integer representing the minimum time to reach home.

If it is impossible to reach home according to Mr. M's demands, output -1 instead.

Sample Input

2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0

Sample Output

100
90
540

题目大意:
有一个n个城市的图,有m条边,对于每个城市的领导者,1城市的领导者一定是1,2城市的领导者一定是2,在保证只有一次从1到2的路的情况下,问从城市1到城市2的最短路径。

思路:

我们可以这样来想这个问题,我们从城市从城市1到城市2无非就是这样走:从城市1走多个(也可以不走)同样是领导者1领导的城市,然后有一次从领导者1的城市到领导者2所领导的城市,然后再在领导者2所领导的城市中最终走到城市2.

我门大可这样来建图:

从城市(领导者为1)1到城市(领导者为1)1的图是双向边,从城市(领导者为2)2到城市(领导者为2)2的图也是双向边,然后从城市(领导者为1)1到城市(领导者为2)2的路只建从城市1到城市2的边。

那么我们就能够保证从领导者为1的城市到达了领导者为2的城市之后,不会有回到城市领导者为1的边供我们走。

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct path
{
    int x;int y;int w;
}a[100000];
int n;
int group[605];
int map[605][605];
void floyd()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=n;k++)
            {
                map[j][k]=min(map[j][k],map[j][i]+map[i][k]);
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        int m;
        scanf("%d",&m);
        memset(map,0x3f3f3f3f,sizeof(map));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&group[i]);
        }
        for(int i=0;i<m;i++)
        {
            int u=a[i].x;int v=a[i].y;
            if(group[u]==group[v])
            {
                //printf("%d %d\n",u,v);
                map[u][v]=map[v][u]=a[i].w;
            }
            else
            {
                if(group[u]==1)
                {
                    map[u][v]=a[i].w;
                }
                else
                {
                    map[v][u]=a[i].w;
                }
            }
        }
        floyd();
        if(map[1][2]==0x3f3f3f3f)printf("-1\n");
        else
        printf("%d\n",map[1][2]);
    }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值