POJ3268 Silver Cow Party(最短路问题)
来源:互联网 发布:快速弄到钱网络偏门2万 编辑:程序博客网 时间:2024/05/17 09:05
Description
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?
Input
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
Source
题目大意:一共有n个点,有一个点为X,且m条边都是单向边,问你从任何点到x加上x回到任何点的最短路里面,最长的多少
解题思路:这是一个最短路问题,由于从x回到各个点的路上是单源到其他点的最短路,所以可以用Bellman-Ford的算法,而各个点到x的路径可以用Ford算法,但是考虑到时间复杂度的问题,就改用反建图以此编程跟单源到其他点一模一样的了
#include<iostream> #include<cstdio> #include<stdio.h> #include<cstring> #include<cstdio> #include<climits> #include<cmath> #include<vector> #include <bitset> #include<algorithm> #include <queue> #include<map> using namespace std;#define inf 9999999;struct point{int x, k;}p;vector<point> tu[1005];queue<int> qua;vector<point> t[1005];long long int ans, b[1005], a[1005];int n, m, x, xx, y, i, k, j;bool check[1005];int main(){cin >> n >> m >> x;memset(a, 0, sizeof(a));for (i = 1; i <= n; i++){b[i] = inf;a[i] = inf;}b[x] = 0;a[x] = 0;for (i = 1; i <= m; i++){cin >> xx >> y >> k;p.x = y;p.k = k;tu[xx].push_back(p);p.x = xx;t[y].push_back(p);}qua.push(x);memset(check, 0, sizeof(check));while (!qua.empty()){k= qua.front();check[k] = false;qua.pop();for (i = 0; i < tu[k].size(); i++){p = tu[k][i];if (p.k + b[k] < b[p.x]){b[p.x] = p.k + b[k];if (check[p.x] != true){check[p.x] = true;qua.push(p.x);}}}}qua.push(x);memset(check, 0, sizeof(check));while (!qua.empty()){k = qua.front();check[k] = false;qua.pop();for (i = 0; i < t[k].size(); i++){p = t[k][i];if (p.k + a[k] < a[p.x]){a[p.x] = p.k + a[k];if (check[p.x] != true){check[p.x] = true;qua.push(p.x);}}}}ans = 0;for (i = 1; i <= n; i++){ans = max(ans,b[i] + a[i]);}cout << ans << endl;}
- POJ3268 Silver Cow Party(最短路问题)
- POJ3268 Silver Cow Party DIJ最短路
- POJ3268 Silver Cow Party 最短路
- 【POJ3268】Silver Cow Party 最短路
- poj3268 Silver Cow Party(最短路变形)
- poj3268 Silver Cow Party【最短路】
- POJ3268—Silver Cow Party(反向最短路)
- POJ3268 Silver Cow Party spfa求解 最短路
- POJ3268 Silver Cow Party(最短路径)
- POJ3268:Silver Cow Party
- poj3268 - Silver Cow Party
- POJ3268 Silver Cow Party
- POJ3268 Silver Cow Party
- POJ3268 Silver Cow Party
- poj3268-Silver Cow Party
- POJ3268-Silver Cow Party
- poj3268 Silver Cow Party
- poj3268 -- Silver Cow Party
- 找出其中有多少种字符,每种字符有几个,并打印出来
- A Dangerous Maze ( 期望 )
- Reorder List
- java串口通信之帧解析
- MySQL 5.7.18免安装版教程
- POJ3268 Silver Cow Party(最短路问题)
- PuTsangTo-单撸游戏开发02 测试场景与单轴移动
- Cookie与Session的区别
- 数组中只出现一次的数
- Javaweb开发入职第一天
- 解决使用小米手机调试android开发程序的问题
- 农村,是否值得你留恋?
- 总结jQuery的基础知识
- 方法重写Override笔记及其与Overload(重载)的区别