九度 题目1162:I Wanna Go Home
来源:互联网 发布:smtp.gmail.com 端口 编辑:程序博客网 时间:2024/05/17 11:04
九度 题目1162:I Wanna Go Home
原题OJ链接:http://ac.jobdu.com/problem.php?pid=1162
题目描述:
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
解题思路:
因为主人公不想来回穿越两军的地盘,但要从1到2,所以要想办法把阵营2到阵营1的路径设为不可到达,对跨越两个阵营的边只保存单向边即可。我在这是把边权值设为INF(999999999)办法,求最短路时判INF跳过。图构建完之后再用Dijsktra最短路算法求得最短路。
源代码:
#include<iostream>#include<vector>using namespace std;#define INF 999999999struct E{ int next; int c;};vector<E> edge[601];bool mark[601];int Dis[601];int owner[601];int main(){ int N,M; while(cin>>N && N!=0){ cin>>M; for(int i=1;i<=N;i++) edge[i].clear(); int A,B,T; for(int i=0;i<M;i++){ cin>>A>>B>>T; E tmp; tmp.next=B; tmp.c=T; edge[A].push_back(tmp); tmp.next=A; edge[B].push_back(tmp); } for(int i=1;i<=N;i++){ cin>>owner[i]; Dis[i]=-1; mark[i]=false; } for(int i=1;i<=N;i++){ if(owner[i]==2){ for(int j=0;j<edge[i].size();j++){ if(owner[edge[i][j].next]==1){ edge[i][j].c=INF; } } } } Dis[1]=0; mark[1]=true; int newP=1; for(int i=1;i<N;i++){ for(int j=0;j<edge[newP].size();j++){ int t=edge[newP][j].next; int c=edge[newP][j].c; if(mark[t]==true) continue; if(c==INF) continue; if(Dis[t]==-1 || Dis[t]>Dis[newP]+c){ Dis[t]=Dis[newP]+c; } } int min=123123123; for(int j=1;j<=N;j++){ if(mark[j]==true) continue; if(Dis[j]==-1) continue; if(Dis[j]<min){ min=Dis[j]; newP=j; } } mark[newP]=true; } cout<<Dis[2]<<endl; } return 0;}
- 九度OJ 题目1162:I Wanna Go Home
- 九度 oj 题目1162:I Wanna Go Home
- 九度 题目1162:I Wanna Go Home
- 题目1162:I Wanna Go Home
- 题目1162:I Wanna Go Home
- 题目1162:I Wanna Go Home
- Jobdu 题目1162:I Wanna Go Home
- 题目1162:I Wanna Go Home
- 题目1162:I Wanna Go Home
- 九度OJ 1162 I Wanna Go Home
- 九度:1162<I wanna go home><最短路径>
- 九度 OJ 1162 I Wanna Go Home
- 九度OJ-1162:I Wanna Go Home
- 最短路径--九度1162.I Wanna Go Home
- 题目1162:I Wanna Go Home:标准单源最短路
- 九度 OJ 1162 I Wanna Go Home 最短路问题
- 九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)
- I wanna go home
- Windows 下WAMP环境的搭建
- 170915 逆向-问鼎杯题库(小磊生病了)
- [二进制分组] BZOJ4140. 共点圆加强版
- 测试一下,打扰了!!!!!!
- MyBatis的配置文件
- 九度 题目1162:I Wanna Go Home
- 关于Form_Load和Public Form()那些事
- 最全Pycharm教程(43)——Pycharm扩展功能之UML类图使用
- LINTCODE——交换链表当中两个节点
- 期望与概率
- 八数码问题(hash+bfs)
- HDU 5924 Mr. Frog’s Problem
- ccf 炉石传说
- Android面试题 你经常写复杂的界面,那Android刷新Ui有什么特点?在这个过程中做哪些操作会出现问题