【考研每日一題22】I Wanna Go Home(C++)

原題地址:牛客網

題目描述:

    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

分析:

題目大意是,有個人要從1走到2,然後呢1和2周圍是有很多城市的,會給你每個城市之間的距離,且這些城市其實是有領導者的,領導者有1和2,走不同領導者的陣營的情況只可以出現一次(意思是如果你一開始走了1->2,那之後必須全部在2的陣營走。)求從城市1到城市2的最短距離。

這其實是一個帶有條件的無向圖的最短路問題。

用迪傑斯特拉可以解出。

(友情提醒,前幾次wa只是因爲少了個break)

代碼:

#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;
#define INF 9999999
int d[601][601];//記錄每個城市之間距離
int g[601];//記錄每個城市的領導者
int dis[601];
int book[601];
int n,m;
void dijkstra()
{
    for(int i=1;i<=n;i++)
    {
        dis[i]=d[1][i];
    }
    memset(book,0,sizeof(book));
    int minn;
    for(int i=1;i<n;i++)
    {
        int jud=1;
        minn=INF;
        int u;
		for(int j=1;j<=n;j++){
			if(book[j]==0&&dis[j]<minn){
				minn=dis[j];
				u=j;
			}
		}
		if(g[u]!=g[1])jud=2;
		book[u]=1;
		if(u==2)break;
		for(int v=1;v<=n;v++){
            if(jud==1||(jud==2&&g[v]==g[u])){
                if(d[u][v]<INF){
                    if(dis[v]>dis[u]+d[u][v])
                        dis[v]=dis[u]+d[u][v];
                }
            }
		}
    }
}
int main()
{
    while(cin>>n)
    {
        if(n==0)break;
        cin>>m;
        memset(d,INF,sizeof(d));
        memset(g,INF,sizeof(g));
        memset(dis,INF,sizeof(dis));
        int a,b,s;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>s;
            d[a][b]=d[b][a]=min(s,d[a][b]);
        }
        for(int i=1;i<=n;i++)
        {
            d[i][i]=0;
            cin>>s;
            g[i]=s;
        }
        dijkstra();
        if(dis[2]==d[1][2])cout<<"-1"<<endl;
        else cout<<dis[2]<<endl;
    }
    return 0;
}

2020.4.10

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章