I Wanna Go Home(Dijkstra)--C++实现

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/zhang__shuang_/article/details/86747674

题目描述

    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.

输入

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

C++实现:

#include<iostream>
#include<vector>
using namespace std; 
struct E{
    int next;
    int cost;
};
vector<E>edge[10001];
int Dis[601];
int co[601];
bool mark[601];
int main(){
    int n,m;
    while(scanf("%d",&n)!=EOF&&n!=0){
        for(int i=1;i<=n;i++){
            edge[i].clear();
        }
        cin>>m;
        for(int i=1;i<=m;i++){
            int a,b,c;
            cin>>a>>b>>c;
            E tmp;
            tmp.next=b;
            tmp.cost=c;
            edge[a].push_back(tmp);
            tmp.next=a;
            edge[b].push_back(tmp);
        }
        for(int i=1;i<=n;i++){
            cin>>co[i];
        }
        /*
        for(int i=1;i<n;i++){
            for(int j=i+1;j<=n;j++){
                if(co[i]!=co[j]){
                    if(co[i]==1){
                        edge[j].erase(edge[j].begin()+i,edge[j].begin()+i+1);
                    }
                    else
                        edge[i].erase(edge[i].begin()+j,edge[i].begin()+j+1);
                }
            }
        }*/
        for(int i=1;i<=n;i++){
            mark[i]=false;
            Dis[i]=-1;
        }
        Dis[1]=0;
        mark[1]=true; 
        int newP=1;
        for(int i=1;i<=n;i++){
            for(int j=0;j<edge[newP].size();j++){
                int p=edge[newP][j].next;
                int q=edge[newP][j].cost;
                if(mark[p]==true)continue;
                //只需在此进行判断即可
                if(co[newP]==2&&co[p]==1)continue;
                if(Dis[p]==-1||Dis[p]>Dis[newP]+q){
                    Dis[p]=Dis[newP]+q;
                }
            }
            int min=123123123;
            for(int i=1;i<=n;i++){
                if(mark[i]==true)continue;
                if(Dis[i]==-1)continue;
                if(min>Dis[i]){
                    min=Dis[i];
                    newP=i;
                }
            }
            mark[newP]=true;
        }
        cout<<Dis[2]<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/zhang__shuang_/article/details/86747674