首页 > 试题广场 >

I Wanna Go Home

[编程题]I Wanna Go Home
  • 热度指数:9501 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    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
头像 Widdit
发表于 2022-02-27 20:34:50
首先在逻辑上将所有顶点划为 2 个阵营,使用 Dijkstra 算法分别计算 2 个阵营内部的最短路径,其中,阵营 1 以顶点 1 为源点,阵营 2 以顶点 2 为源点。 然后遍历所有“跨域边”,找到该边连接的两点分别离顶点 1 和顶点 2 的最短路径,再加上这条边的长度,就是 M 先生回家 展开全文
头像 健康快乐最重要
发表于 2020-02-24 13:06:17
分析了半天题的情况,最后被大佬的一句话点醒了。大佬原话: 没错,一开始我的分析是,因为1城市是1阵营,2城市是2阵营,所以只要保证连接1和2中间的城市阵营都相等就可以。比如1 2 2 2 2或者1 1 1 1 2,但是万万漏掉了一点就是 可能有1 1 2 2 2这样的情况,即中间不是规律的,但是最 展开全文
头像 鱼儿恋上水
发表于 2020-04-16 13:31:04
Dijkstra算法 ①对跨越两个阵营的边只保留单向边 #include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int MAXV = 1010; c 展开全文
头像 土尔逊Torson
发表于 2023-07-07 23:05:22
//土尔逊Torson 编写于2023/07/07 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <vector> #include <queue 展开全文
头像 求求offer的土拨鼠很无聊
发表于 2023-03-20 11:21:46
#include <queue> #include <iostream> #include <vector> using namespace std; const int maxn=1000; const int MAX=10000; struct edge{ 展开全文
头像 flyflyfly00
发表于 2021-04-21 22:14:01
http://poj.org/problem?id=3767AC代码 #include <iostream> using namespace std; const int N = 601; const int INF = 10000000; int n, m; int path[N 展开全文
头像 小花Student
发表于 2024-03-02 20:54:28
#include<iostream> #include<algorithm> const int MAX = 1024; const int INF = 1e9; class Solution { public: int n; int G[MAX] 展开全文
头像 Huster水仙
发表于 2023-02-06 23:30:11
Dijkstra算法(优先队列优化) 注意:we assume that Mr. M starts from city 1 and his target is city 2. 合法路径:1→1,1→2,2→2 非法路径:2→1 距离更新时增加限制:禁止从2转向1 #include< 展开全文
头像 阿尔芒a
发表于 2024-03-24 18:12:32
#include <iostream> #include <string> #include <vector> #include<queue> using namespace std; const int INF = 1e6; const int M 展开全文
头像 牛客567628359号
发表于 2023-03-27 10:58:28
#include <climits> #include <iostream> #include <vector> #include <queue> using namespace std; struct Edge{ int to,w; 展开全文

问题信息

难度:
86条回答 8538浏览

热门推荐

通过挑战的用户

查看代码