POJ - 3767 - I Wanna Go Home
2016-04-26 23:31
525 查看
I Wanna Go Home
题目描述
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.
样例输入
21
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
样例输出
10090
540
思路:刚开始真想不出来,考虑的很复杂,最后参考了网上的题解后恍然大悟,对于不是同一个阵营的路换成单向的就简单了,同一个阵营的路还是双向路
#include<cstdio> #include<cstring> #include<algorithm> #define INF 0xffffff #define MAXN 605 using namespace std; int mp[MAXN][MAXN], dis[MAXN], team[MAXN]; bool vis[MAXN]; void dijkstra(int s, int n){ memset(vis, false, sizeof(vis)); for(int i = 1; i <= n; i++) dis[i] = mp[s][i]; vis[s] = true; dis[s] = 0; for(int i = 1; i < n; i++){ int min = INF, pos; for(int j = 1; j <= n; j++){ if(!vis[j] && dis[j] < min){ min = dis[j]; pos = j; } } if(min == INF) break; vis[pos] = true; for(int j = 1; j <= n; j++){ if(!vis[j] && dis[pos]+mp[pos][j] < dis[j]){ dis[j] = dis[pos] + mp[pos][j]; } } } if(dis[2] < INF) printf("%d\n", dis[2]); else printf("-1\n"); } int main() { int n, m; while(scanf("%d", &n)!=EOF){ if(n == 0) break; scanf("%d", &m); for(int i = 0; i < MAXN; i++) for(int j = 0; j < MAXN; j++) mp[i][j] = INF; for(int i = 0; i < m; i++){ int x, y, t; scanf("%d %d %d", &x, &y, &t); mp[x][y] = mp[y][x] = t; } for(int i = 1; i <= n; i++) scanf("%d", &team[i]); for(int i = 1; i<=n; i++){ for(int j = i+1; j <= n; j++){ if(team[i]==team[j]) //如果是同一个阵营,就不做操作 continue; else if(team[i]==1 && team[j]==2) //如果不是同一个阵营,就简历单向边 mp[j][i] = INF; else if(team[i]==2 && team[j]==1) mp[i][j] = INF; } } dijkstra(1, n); } return 0; }
相关文章推荐
- django中templates阅读笔记
- hdu-1154 Cutting a Polygon(计算几何综合应用,多模板)
- 仿Google应用动态隐藏显示状态栏
- django开发备忘
- Django:之CMDB资源系统
- POJ 1042 Gone Fishing(模拟+贪心)
- Codeforces Round #340 (Div. 2)E - XOR and Favorite Number(Mo's algorithm)
- 线性模型(1):Perceptron Learning Algorithm (PLA)
- beego api
- Introduction to x265 Rate Control Algorithm
- django的class Meta
- GO语言编程-顺序编程之变量
- go 简介学习
- <<wherego>>项目
- [Graduation]《WhereGo》工作计划安排-1
- Django model字段类型清单
- Django模型的Field Types总结
- go语言安装与设置
- HDU 4341 - Gold miner
- go语言特点