题目1162:I Wanna Go Home:标准单源最短路

题目1162:I Wanna Go Home

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:336

解决:159

题目描述:

    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
来源:
2011年北京大学计算机研究生机试真题
dij算法两次,连接处遍历求最小值。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXN 1000
int n,m,a[605][605],camp[605],dia[605],dib[605],min;
void input()
{
    int i,j;
    int x,y,t;
    scanf("%d",&m);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            a[i][j]=MAXN;
        a[i][i]=0;
        dia[i]=dib[i]=MAXN;
    }
    min=MAXN;
    while(m--)
    {
        scanf("%d%d%d",&x,&y,&t);
        a[x-1][y-1]=a[y-1][x-1]=t;
    }
    for(i=0;i<n;i++)
        scanf("%d",&camp[i]);
}
void dijie(int type)
{
    int i,j;
    int mini,p,use[605];
    if(type==1)
    {
        for(i=0;i<n;i++)
        {
            if(camp[i]==1)
            {
                dia[i]=a[0][i];
                use[i]=0;
            }
        }
        use[0]=1;
        while(1)
        {
            mini=MAXN;
            p=-1;
            for(i=0;i<n;i++)
                if(!use[i]&&dia[i]<mini)
                {
                    mini=dia[i];
                    p=i;
                }
                if(p!=-1)
                {
                    use[p]=1;
                    for(i=0;i<n;i++)
                    {
                        if(!use[i]&&a[p][i]+dia[p]<dia[i])
                            dia[i]=a[p][i]+dia[p];
                    }
                }
                else
                    break;
        }
    }
    else
    {
        for(i=0;i<n;i++)
        {
            if(camp[i]==2)
            {
                dib[i]=a[1][i];
                use[i]=0;
            }
        }
        use[1]=1;
        while(1)
        {
            mini=MAXN;
            p=-1;
            for(i=0;i<n;i++)
                if(!use[i]&&dib[i]<mini)
                {
                    mini=dib[i];
                    p=i;
                }
                if(p!=-1)
                {
                    use[p]=1;
                    for(i=0;i<n;i++)
                    {
                        if(!use[i]&&a[p][i]+dib[p]<dib[i])
                            dib[i]=a[p][i]+dib[p];
                    }
                }
                else
                    break;
        }
    }
}
void jiaojie()
{
    int i,j;
    for(i=0;i<n;i++)
    {
        if(camp[i]==1)
        {
            for(j=0;j<n;j++)
                if(camp[j]==2&&a[i][j]<MAXN&&dia[i]+dib[j]+a[i][j]<min)
                    min=dia[i]+dib[j]+a[i][j];
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d",&n)!=EOF&&n)
    {
        input();
        dijie(1);
        dijie(2);
        jiaojie();
        if(min<MAXN)
            printf("%d\n",min);
        else
            printf("-1\n");
    }
    return 0;
}
 
/**************************************************************
    Problem: 1162
    User: smileyk
    Language: C++
    Result: Accepted
    Time:20 ms
    Memory:2456 kb
****************************************************************/


你可能感兴趣的:(题目1162:I Wanna Go Home:标准单源最短路)