I wanna go home(最短路)

本文深入探讨了编程领域的关键组件和技术实践,涵盖了从基础工具到高级概念的广泛范围,旨在帮助开发者构建更高效、安全且易于维护的软件系统。通过详细分析各种开发工具、框架、编程语言及其在不同场景下的应用,本文为读者提供了一个全面的技术地图,助力于提升编程技能和项目成功率。

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

题目描述:

    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

题目大意是:n个城市被分为2个阵营。商人要从城市1走到城市2,要求是只能通过最多一个连接不同阵营的城市的道路。求最短路径
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
struct E
{
    int next;
    int t;
};
vector<E> edge[601];
int Dis[601];
bool mark[601];
int city[601];
int main()
{
    int n,m,a,b,c;
    while(cin>>n>>m,n)
    {
        for(int i=1;i<=n;i++)
        {
            edge[i].clear();//不要忘记清空
            Dis[i]=-1;
            mark[i]=false;
        }
        while(m--)
        {
            cin>>a>>b>>c;
            E tmp;
            tmp.next=b;
            tmp.t=c;
            edge[a].push_back(tmp);
            tmp.next=a;
            edge[b].push_back(tmp);
        }
        for(int i=1;i<=n;i++)
        {
            cin>>city[i];
        }
        Dis[1]=0;
        mark[1]=true;
        int newp=1;
        for(int i=1;i<=n;i++)//既是让city=2的节点有进无出
        {
            if(city[i]==2)
            {
                for(int j=0;j<edge[i].size();j++)
                {
                    if(city[edge[i][j].next]==1)
                        edge[i][j].t=0;
                }
            }
        }
        for(int i=1;i<n;i++)
        {
            for(int j=0;j<edge[newp].size();j++)
            {
                int s=edge[newp][j].next;
                int c=edge[newp][j].t;
                if(mark[s]==true) continue;
                if(c==0) continue;//不要忘记
                if(Dis[s]==-1||Dis[s]>Dis[newp]+c)
                {
                    Dis[s]=Dis[newp]+c;
                }
            }
            int min=333333333;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]==true) continue;
                if(Dis[i]==-1) continue;
                if(Dis[i]<min)
                {
                    min=Dis[i];
                    newp=i;
                }
            }
            mark[newp]=true;
        }
        cout<<Dis[2]<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值