POJ3268 Silver Cow Party 【Dijkstra】

Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13183   Accepted: 5932

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

Line 1: Three space-separated integers, respectively: NM, and X

Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi,
requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

Source

USACO 2007 February Silver

因为是求一个来回,所以构造顺逆两张图,两次Dijkstra即可。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;

#define maxn 1010
#define maxm 100010
#define inf 0x3f3f3f3f

int N, M, X;
int head1[maxn], head2[maxn], id1, id2;
struct Node {
    int v, w, next;
} E1[maxm], E2[maxm];
int dist1[maxn], dist2[maxn];
bool vis1[maxn], vis2[maxn];

void addEdge(int head[], Node E[], int& id, int u, int v, int w) {
    E[id].v = v; E[id].w = w;
    E[id].next = head[u]; head[u] = id++;
}

void getMap() {
    int u, v, w; id1 = id2 = 1;
    while(M--) {
        scanf("%d%d%d", &u, &v, &w);
        addEdge(head1, E1, id1, u, v, w);
        addEdge(head2, E2, id2, v, u, w); // reverse
    }
}

int getNext(int dist[], bool vis[]) {
    int u = 0, i, v = inf;
    for(i = 1; i <= N; ++i)
        if(!vis[i] && dist[i] < v) {
            v = dist[i]; u = i;
        }
    return u;
}

void Dijkstra(int source, int head[], Node E[], int dist[], bool vis[]) {
    int i, u = source, v, w;
    memset(dist, 0x3f, sizeof(int) * (N + 1));
    dist[u] = 0;
    while(u) {
        vis[u] = 1;
        for(i = head[u]; i; i = E[i].next)
            dist[v=E[i].v] = min(dist[v], dist[u] + E[i].w);
        u = getNext(dist, vis);
    }
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    scanf("%d%d%d", &N, &M, &X);
    getMap();
    Dijkstra(X, head1, E1, dist1, vis1);
    Dijkstra(X, head2, E2, dist2, vis2);
    int ans = 0;
    for(int i = 1; i <= N; ++i)
        ans = max(ans, dist1[i] + dist2[i]);
    printf("%d\n", ans);
    return 0;
}
时间: 2024-09-30 19:58:42

POJ3268 Silver Cow Party 【Dijkstra】的相关文章

HDU2680 Choose the best route 【Dijkstra】

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7061    Accepted Submission(s): 2300 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU1874 畅通工程续 【Dijkstra】

畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 26970    Accepted Submission(s): 9719 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走

HDU2544 最短路 【Dijkstra】

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31182    Accepted Submission(s): 13456 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

POJ3270 cow sorting 【polya】

题目描述: 给你一个数字序列(每个数字唯一),每次你可以交换任意两个数字,代价为这两个数字的和,问最少用多少代价能把这个序列按升序排列好. 题目的具体做法是参考刘汝佳的<算法艺术与信息学奥赛>大概思路是:以后再用别种方法解, 1.找出初始状态和目标状态.明显,目标状态就是排序后的状态. 2.画出置换群,在里面找循环.例如,数字是8 4 5 3 2 7 明显,                                   目标状态是2 3 4 5 7 8,能写为两个循环:(8 2 7)(4

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

HDOJ 1874 畅通工程续 【dijkstra】

题意:... 策略:最简单的求最短路径. 代码: #include<stdio.h> #include<string.h> #define MAXN 1005 #define INF 0x3f3f3f3f int di[MAXN], vis[MAXN], n, m; int map[MAXN][MAXN]; void dijkstra(int v) { int i, j; memset(vis, 0, sizeof(vis)); di[v] = 0; vis[v] = 1; for

HDU3790 最短路径问题 【Dijkstra】

最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13336    Accepted Submission(s): 4072 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Input

HDU2112 HDU Today 【Dijkstra】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14743    Accepted Submission(s): 3471 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候