POJ 3268 Silver Cow Party
来源:互联网 发布:丽得姿和美迪惠尔 知乎 编辑:程序博客网 时间:2024/06/05 08:59
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; roadi 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?
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers:Ai, Bi, andTi. The described road runs from farmAi to farm Bi, requiringTi time units to traverse.
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
10
题目大意:
在一张有向图中n - 1头牛去另外一头牛家参加派对, 并会在结束后返回自己的家中, 但是他们都很懒, 所以会走效率高的路(尽可能短的路), 请求出走得最远的牛走了多远。
解题思路:
一开始用Dijkstra傻傻的从单源点向其他点跑和从其他点向单源点跑, 妥妥TLE, 按点跑Dijkstra的时间复杂度是O(N^2)。 于是我们可以开两个数组, 一个记录所有牛回家的距离, 一个记录所有牛走的距离。 两个距离相加取最大值, 对于从其他点向单源点跑的操作, 我们可以对于初始矩阵进行矩阵转置操作, 相当于在反着跑一边。O(2 * N)。第二种解法可以用优先队列, 按着边开始跑,时间复杂度为O(N * (Elog(E)))。
代码:
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
*ios::sync_with_stdio(false);
*/
typedef unsigned long long ull;
const int inf = 0x7fffffff;
const int mod = 1000000;
const int Max = 1010;
int Map[Max][Max][2];
bool vis[Max];
int dis_c[Max], dis_g[Max];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
Map[i][j][0] = (i == j) ? 0 : inf;
//Map[j][i][0] = (i == j) ? 0 : inf;
Map[i][j][1] = (i == j) ? 0 : inf;
//Map[j][i][1] = (i == j) ? 0 : inf;
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
Map[i][j][0] = Map[j][i][1];
}
}
}
int dis[Max];
for (int i = 1; i <= n; ++i) {
dis[i] = Map[st][i][f];
vis[i] = 0;
}
vis[st] = 1;
for (int i = 1; i <= n; ++i) {
int Min = inf, mark;
for (int j = 1; j <= n; ++j) {
if (!vis[j] && Min > dis[j]) {
Min = dis[mark = j];
}
}
vis[mark] = true;
for (int j = 1; j <= n; ++j) {
// that's very important
if (!vis[j] && Map[mark][j][f] != inf && dis[j] > dis[mark] + Map[mark][j][f]) {
dis[j] = dis[mark] + Map[mark][j][f];
}
}
}
if (f == 0) {
for (int i = 1; i <= n; ++i) {
dis_c[i] = dis[i];
}
}
else{
for (int i = 1; i <= n; ++i) {
dis_g[i] = dis[i];
}
}
}
int ans = -1;
for (int i = 1; i <= n; ++i) {
ans = max(ans, (dis_c[i] + dis_g[i]));
}
printf("%d\n", ans);
}
// initialization
scanf("%d %d %d", &n, &m, &x);
Init();
for (int i = 0; i < m; ++i) {
int x, y, t;
scanf("%d %d %d", &x, &y, &t);
if (Map[x][y][1] > t)
Map[x][y][1] = t;
}
// back
Dijkstra(x, 1);
// a to b >> b to a
map_Swap();
// go
Dijkstra(x, 0);
// output the answer
check();
return 0;
}
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
*ios::sync_with_stdio(false);
*/
typedef unsigned long long ull;
const int inf = 0x7fffffff;
const int mod = 1000000;
const int Max = 1010;
int dis[Max];
bool vis[Max];
struct node {
// endpoint and distance
int x, d;
node() {};
node(int a, int b) {
x = a;
d = b;
}
bool operator < (const node &a) const {
if (d == a.d) return x < a.x;
else return d > a.d;
}
};
vector < node> edge[Max];
for (int i = 0; i <= n; ++i) {
edge[i].clear();
}
}
for (int i = 0; i <= n; ++i) {
dis[i] = inf;
}
dis[st] = 0;
priority_queue < node> Q;
Q.push(node(st, dis[st]));
while (!Q.empty()) {
node x = Q.top();
Q.pop();
for (int i = 0; i < edge[x.x].size(); ++i) {
node y = edge[x.x][i];
if (dis[y.x] > x.d + y.d) {
dis[y.x] = x.d + y.d;
Q.push(node(y.x, dis[y.x]));
}
}
}
return dis[ed];
}
int ans = 0, minn;
for (int i = 1; i <= n; ++i) {
minn = Dijkstra(x, i) + Dijkstra(i, x);
if (minn > ans)
ans = minn;
}
printf("%d\n", ans);
}
// initialization
scanf("%d %d %d", &n, &m, &x);
Init();
int x, y, t;
scanf("%d %d %d", &x, &y, &t);
edge[x].push_back(node(y, t));
}
// perform the algorithm
check();
return 0;
}
*/
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- POJ-3268-Silver Cow Party
- poj 3268--Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ 3268 Silver Cow Party
- POJ-3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- poj 3268 Silver Cow Party
- design editor is unavailable until a successful build
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- 51Nod-1829-函数
- 如何给电脑硬盘分区
- git总结之2: 必须的配置,仅需一次
- POJ 3268 Silver Cow Party
- 初体验——DataBinding (一)
- 国内外常用OJ介绍
- JavaSE 两数交换
- Activation HDU
- 前端之js-echarts(二)
- 【js】使用xml数据载体实现城市省份二级联动
- Newtonsoft.Json简要使用笔记
- 7.1 键盘获取3个整型数据