POJ 3767 I Wanna Go Home【最短路,Dijkstra+spfa,题意是关键呀】

本文介绍了一个涉及图论和算法的问题,即在一个城市被分为两个阵营的国家中,找到从一个城市到另一个城市的最短路径,路径中最多只能包含一条连接不同阵营城市的道路。通过使用Dijkstra算法和SPFA算法解决这一挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I Wanna Go Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3499 Accepted: 1504

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

原题链接:http://poj.org/problem?id=3767

题意:一个国家有n个城市,之间共有m条路,每座城市属于group1或group2,1一定属于group1 , 2一定属于group2,属于group2 的城市不能直接到达属于group1的城市,问从1到2的最短路。

有人说建图是关键,但我认为,松弛操作时才是关键。建图双向边单向边不好控制。

如果均建双向边,求解的过程中避免直接从group2到group1即可。

理解题意后DIjkstra算法和spfa算法随便来。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int maxn=605;
const int INF=0x3f3f3f3f;
int a[maxn][maxn];
int dis[maxn];
bool vis[maxn];
int fa[maxn];
int n,m;
void Dijkstra()
{
    for(int i=1; i<=n; i++)
    {
        dis[i]=a[1][i];
        vis[i]=false;
    }
    dis[1]=0;
    vis[1]=true;
    for(int i=0; i<n; i++)
    {
        int minn=INF;
        int p=-1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<minn)
                minn=dis[p=j];
        }
        if(p==-1)
            break;
        vis[p]=true;
        for(int j=1; j<=n; j++)
        {
            if(fa[p]==2&&fa[j]==1)
                continue;
            if(!vis[j]&&dis[j]>dis[p]+a[p][j])
                dis[j]=dis[p]+a[p][j];
        }
    }
}
void spfa()
{
    for(int i=1; i<=n; i++)
    {
        dis[i]=INF;
        vis[i]=false;
    }
    dis[1]=0;
    vis[1]=true;
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        vis[p]=false;
        for(int i=1; i<=n; i++)
        {
            if(fa[p]==2&&fa[i]==1)
                continue;
            if(dis[i]>dis[p]+a[p][i])
            {
                dis[i]=dis[p]+a[p][i];
                if(!vis[i])
                {
                    vis[i]=true;
                    q.push(i);
                }
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("data/3767.txt","r",stdin);
    while(cin>>n,n)
    {
        cin>>m;
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=n; j++)
                if(i==j) a[i][j]=0;
                else a[i][j]=INF;
        }
        int x,y,z;
        while(m--)
        {
            cin>>x>>y>>z;
            a[x][y]=a[y][x]=z;
        }
        for(int i=1; i<=n; i++)
            cin>>fa[i];
        //Dijkstra();
        spfa();
        if(dis[2]<INF)
            cout<<dis[2]<<endl;
        else
            cout<<"-1"<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值