I want to go home

在一场内战中,城市被分裂成两派。商人Mr.M必须在遵守仅能通过一条连接不同阵营城市的道路的规定下尽快回家。本篇介绍了一个算法解决方案,用于找到满足条件的最短路径。

摘要生成于 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.
示例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所了领导的城市,我们可以设置标记
#include<iostream>
#include<vector>
using namespace std;
struct E
{
    int next;//一个存储相邻的结点
    int cost;//存储边的权值
};


vector<E>edges[10001];
int dis[700];
int leader[700];
bool mark[701];//标记某顶点是否在最短路径顶点集合当中
int newp;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)
        {
            edges[i].clear();//每一个结点保存着其相邻的边
            mark[i]=false;
            dis[i]=-1;
        }
        for(int i=1;i<=m;i++)
        {
           E n;
           int a,b,cost;
           cin>>a>>b>>cost;
           n.next=b;
           n.cost=cost;
           edges[a].push_back(n);
           n.next=a;
           edges[b].push_back(n);
        }//这段是建立无向图的,任意一条边连接着两个顶点
        for(int i=1;i<=n;i++)
            cin>>leader[i];
        mark[1]=true;
        newp=1;
        dis[1]=0;//存储的权值(到第一个点权值和)
        for(int i=1;i<n;i++)//除了最后一个点
        {
            for(int j=0;j<edges[newp].size();j++)
            {
                int t=edges[newp][j].next;
                int c=edges[newp][j].cost;
                if(mark[t]==true)continue;//已经在路径集合当中
                if(leader[t]==1&&leader[newp]==2)continue;
                if(dis[t]==-1||dis[t]>c+dis[newp])
                {
                    dis[t]=dis[newp]+c;
                }
            }//找到newp到其每个相邻点的权值
            //求权值最小值并将那个点置为newp
                int minn=10000;
                for(int j=1;j<=n;j++)
                {
                    if(mark[j])continue;
                    if(dis[j]==-1)continue;
                    if(minn>dis[j])
                    {
                        minn=dis[j];
                        newp=j;
                    }
                }
                mark[newp]=true;
        }
        cout<<dis[2]<<endl;


    }
    return 0;


}


v1 v2 ham Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat... ham Ok lar... Joking wif u oni... spam Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's ham U dun say so early hor... U c already then say... ham Nah I don't think he goes to usf, he lives around here though spam FreeMsg Hey there darling it's been 3 week's now and no word back! I'd like some fun you up for it still? Tb ok! XxX std chgs to send, 螢1.50 to rcv ham Even my brother is not like to speak with me. They treat me like aids patent. ham As per your request 'Melle Melle (Oru Minnaminunginte Nurungu Vettam)' has been set as your callertune for all Callers. Press *9 to copy your friends Callertune spam WINNER!! As a valued network customer you have been selected to receivea 螢900 prize reward! To claim call 09061701461. Claim code KL341. Valid 12 hours only. spam Had your mobile 11 months or more? U R entitled to Update to the latest colour mobiles with camera for Free! Call The Mobile Update Co FREE on 08002986030 ham I'm gonna be home soon and i don't want to talk about this stuff anymore tonight, k? I've cried enough today. spam SIX chances to win CASH! From 100 to 20,000 pounds txt> CSH11 and send to 87575. Cost 150p/day, 6days, 16+ TsandCs apply Reply HL 4 info 以上格式的数据有3000条,运用朴素贝叶斯模型对以上格式的垃圾邮件进行分析,并写出详细代码
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值