POJ 3767 I Wanna Go Home【最短路floyd】
来源:互联网 发布:阿铭linux 编辑:程序博客网 时间:2024/05/22 16:51
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 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.
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
211 2 1001 2331 2 1001 3 402 3 501 2 1553 1 2005 3 1502 5 1604 3 1704 2 1701 2 2 2 10
Sample Output
10090540
题目大意:
有一个n个城市的图,有m条边,对于每个城市的领导者,1城市的领导者一定是1,2城市的领导者一定是2,在保证只有一次从1到2的路的情况下,问从城市1到城市2的最短路径。
思路:
我们可以这样来想这个问题,我们从城市从城市1到城市2无非就是这样走:从城市1走多个(也可以不走)同样是领导者1领导的城市,然后有一次从领导者1的城市到领导者2所领导的城市,然后再在领导者2所领导的城市中最终走到城市2.
我门大可这样来建图:
从城市(领导者为1)1到城市(领导者为1)1的图是双向边,从城市(领导者为2)2到城市(领导者为2)2的图也是双向边,然后从城市(领导者为1)1到城市(领导者为2)2的路只建从城市1到城市2的边。
那么我们就能够保证从领导者为1的城市到达了领导者为2的城市之后,不会有回到城市领导者为1的边供我们走。
AC代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;struct path{ int x;int y;int w;}a[100000];int n;int group[605];int map[605][605];void floyd(){ for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { for(int k=1;k<=n;k++) { map[j][k]=min(map[j][k],map[j][i]+map[i][k]); } } }}int main(){ while(~scanf("%d",&n)) { if(n==0)break; int m; scanf("%d",&m); memset(map,0x3f3f3f3f,sizeof(map)); for(int i=0;i<m;i++) { scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); } for(int i=1;i<=n;i++) { scanf("%d",&group[i]); } for(int i=0;i<m;i++) { int u=a[i].x;int v=a[i].y; if(group[u]==group[v]) { //printf("%d %d\n",u,v); map[u][v]=map[v][u]=a[i].w; } else { if(group[u]==1) { map[u][v]=a[i].w; } else { map[v][u]=a[i].w; } } } floyd(); if(map[1][2]==0x3f3f3f3f)printf("-1\n"); else printf("%d\n",map[1][2]); }}
- POJ 3767 I Wanna Go Home【最短路floyd】
- POJ 3767I Wanna Go Home(最短路/Floyd)
- POJ 3767 I Wanna Go Home SPFA最短路
- POJ 3767 I Wanna Go Home 最短路
- poj 3767 I Wanna Go Home 最短路spfa
- I wanna go home(最短路)
- POJ 3767 I Wanna Go Home【最短路,Dijkstra+spfa,题意是关键呀】
- POJ 3767 I Wanna Go Home 笔记 最短路径
- POJ 3767 I Wanna Go Home
- POJ 3767 I Wanna Go Home
- poj 3767 I Wanna Go Home
- poj 3767 I Wanna Go Home
- POJ 3767 I Wanna Go Home
- poj 3767 I Wanna Go Home
- POJ 3767 I Wanna Go Home
- POJ 3767 I Wanna Go Home(Dijkstra)
- POJ - 3767 - I Wanna Go Home
- I wanna go home
- (枚举)算法竞赛入门经典(7.1.2)最大乘积
- JnI调用机制
- 分享一个链接
- Html之表单标签
- 在类中声明函数而不去定义编译通过
- POJ 3767 I Wanna Go Home【最短路floyd】
- 查找(一)史上最简单清晰的红黑树讲解
- bfs
- iOS开发数据库篇—FMDB简单介绍
- CodeForces - 584C Marina and Vasya (模拟)找规律
- java操作properties文件
- LeetCode *** 38. Count and Say
- iOS开发数据库篇—FMDB数据库队列
- MTK BT/WIFI小析