Silver Cow Party
来源:互联网 发布:js实现点击重置按钮 编辑:程序博客网 时间:2024/05/18 00:24
题目:One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
思路 本题采用了Dijkstra算法(单源最短路径),首先要清楚最短路径的最优子结构:如果P(i,j)={Vi….Vk..Vs…Vj}是从顶点i到j的最短路径,k和s是这条路径上的一个中间顶点,那么P(k,s)必定是从k到s的最短路径。而Dijkstra算法基于此原理,进行最短路径的递增,并最终生成最短路径。对于原节点,选择与其相邻且路径最短的节点,dist[i] = dist[k] + map[k][i],后面的节点依据这种思路进行递增。
Dijkstra算法参考博客:http://www.cnblogs.com/dolphin0520/archive/2011/08/26/2155202.html
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int MAXN = 1010;const int INF = 0x3f3f3f3f;bool visited[MAXN];void Dijkstra(int map[][MAXN], int dist[], int n, int v0){ for (int i = 1; i <= n; i++) { dist[i] = INF; visited[i] = false; } dist[v0] = 0; for (int j = 0; j<n; j++) { int k = -1; int Min = INF; for (int i = 1; i <= n; i++) if (!visited[i] && dist[i]<Min) { Min = dist[i]; k = i; } if (k == -1)break; visited[k] = true; for (int i = 1; i <= n; i++) if (!visited[i] && dist[k] + map[k][i]<dist[i]) dist[i] = dist[k] + map[k][i]; }}int dist1[MAXN];int dist2[MAXN];int map[MAXN][MAXN];int main(){ int N, M, X; int u, v, w; scanf("%d%d%d", &N, &M, &X) == 3; for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) { if (i == j)map[i][j] = 0; else map[i][j] = INF; } while (M--) { scanf("%d%d%d", &u, &v, &w); map[u][v] = min(map[u][v], w); } Dijkstra(map, dist1, N, X); for (int i = 1; i <= N; i++) for (int j = 1; j<i; j++) swap(map[i][j], map[j][i]); Dijkstra(map, dist2, N, X); int ans = 0; for (int i = 1; i <= N; i++) ans = max(ans, dist1[i] + dist2[i]); printf("%d\n", ans); return 0;}
- Silver Cow Party
- POJ3268:Silver Cow Party
- poj3268 - Silver Cow Party
- POJ3268 Silver Cow Party
- POJ3268 Silver Cow Party
- Silver Cow Party
- poj_3268 Silver Cow Party
- POJ3268 Silver Cow Party
- K - Silver Cow Party
- poj3268-Silver Cow Party
- Silver Cow Party
- Silver Cow Party
- Silver Cow Party
- poj Silver Cow Party
- POJ3268-Silver Cow Party
- poj Silver Cow Party
- poj3268 Silver Cow Party
- Silver Cow Party
- docker centos redis 基础常用命令
- 【PAT 1004 Acute Stroke (30)】& dfs
- 【PAT】【Advanced Level】1095. Cars on Campus (30)
- BeautifulSoup库中find_all()方法
- 2017最新java学习大全
- Silver Cow Party
- Retrofit和Rxjava的简单了解
- tf-idf:sklearn中TfidfVectorizer使用
- spring-task
- 【Java】split(".")
- Attack on Titans ZOJ
- czl蒻蒟的OI之路2
- server-send node.js实现
- UVA11584[Partitioning by Palindromes] 动态规划