I Wanna Go Home

题目描述

    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?

输入描述:

    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 A, B and T, which means the road between city A and city B will cost time T. T 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.

输出描述:

    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.

示例1

输入

复制

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

输出

复制

100
90
540

题目分析:本题主要是求解两点之间的最短路径的问题,因此我们自然可以想到采用迪杰斯特拉算法或者弗洛伊德算法来进行求解(本人采用迪杰斯特拉进行求解)。但是题目中要求在最短的路径中横跨两个阵营的边最多只能有一条,因此我们在构造距离矩阵时,当一边横跨两个不同阵营时,将其设置为阵营一到阵营二的有向边(其余边设置为无向边)。这样的话但路径中可以保证最多只有一条边横跨两个不同阵营。

代码实现:

#include <cstdlib>
#include <cstdio>
#include <iostream>


using namespace std;

#define N 602
#define INF 999999

long  map[N][N];//用于存储距离矩阵
long  dist[N];//用于存储顶点一到各点的距离
int flog[N];//用于存储各点所属的不同阵营
int flog2[N];//用于在迪杰斯特拉算法的判断一个点是否已经加入了集合

long  djskla(int n)
{
    dist[1]=0;//顶点一到自己的距离为0
    flog2[1]=0;//将顶点一加入到已经遍历的集合中
    long  temp=INF;
    for(int i=2;i<=n;i++)
    {
        dist[i]=map[1][i];//初始化dist[]
        flog2[i]=1;//初始化flog2[]
    }
    int k=0;
    for(int i=1;i<=n;i++)
    {
        temp=INF;
        for(int j=2;j<=n;j++)
        {
            if(temp>=dist[j]&&flog2[j]==1)//查找所有未遍历点中距离顶点一最短的点
            {
                temp=dist[j];
                k=j;
            }
        }
        flog2[k]=0;//将该点标记为已经遍历
        if(k==2)//当标记点为顶点2是则表明已经得到dist[2]
            break;
        for(int j=2;j<=n;j++)
        {
            if(dist[j]>(dist[k]+map[k][j]))
            {
                dist[j]=dist[k]+map[k][j];//更新数组dist[]
            }
        }
    }
    return dist[2];
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)&&n!=0)
    {
        int i,j;
        long long cost;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
             if(i==j)
                 map[i][j]=0;
              else map[i][j]=INF;//初始化距离矩阵map[][]
            }
        }
        for(int k=1;k<=m;k++)
        {
            cin>>i>>j>>cost;
            if(map[i][j]>cost){
            map[i][j]=cost;
            map[j][i]=cost;}//根据输入值构造距离矩阵map[][],当作无向图处理
        }
        for(int k=1;k<=n;k++)
        {
            scanf("%d",&flog[k]);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(flog[i]==2&&flog[j]==1)
                {
                    map[i][j]=INF;//因为只能有一条边横跨两个阵营,因此但图中的边横跨两个阵营是将边设置为阵营一到阵营2的有向边
                }
            }
        }
        if(djskla(n)<INF)
            cout<<djskla(n)<<endl;//输出结果
        else cout<<"-1"<<endl;
    }
}

结果展示:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值