poj3268Silver Cow Party(单源最短路径)

本文探讨了一个有趣的最短路径问题:一群来自不同农场的牛需要参加聚会并返回,每头牛都选择最短的时间路径。文章提供了一种解决方案,即使用Dijkstra算法两次来确定从聚会地点到各个农场的往返最短时间,并找出时间最长的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

n个牛要到牛x处聚会,聚会完后回到自己的地方,每个牛走的都是最短路,问哪一个牛走的路用的时间最长。方法:以牛x为起点,通过dijiktra算法就可以求出每个牛的返程路径,但每个牛去牛x的路径不可能用floyd算法,所有我们把所有路径返过来,再以牛x为起点,求一次最短路径,就可以知道每个牛的去程时间了。

Silver Cow PartyTime Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

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 ≤ XN). 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: N, M, and X
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

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.
AC代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define INF 999999999
#define LL long long
#define mod 1000003
using namespace std;
struct Edge
{
	int to, dist;
	Edge(){}
	Edge(int v, int d) :to(v), dist(d) {}
};
struct node
{
    int from,to,dist;
};
node road[100005];//记录路径,之后反过来
int n, m;
vector<Edge> edges[1005];
bool done[1005];
int d[1005];
void init()
{
	for (int i = 0; i <= n; i++)
	edges[i].clear();
}
struct HeapNode
{
	int d,u;
	HeapNode(int dd,int uu)
	{
	    d=dd;
	    u=uu;
	}
	bool operator < (const HeapNode& rhs) const {
		return d>rhs.d;
	}
};
void dijk(int s)
{
    for(int i=1;i<=n;i++)
        d[i]=INF;
    d[s]=0;
	priority_queue<HeapNode>q;
    memset(done, 0, sizeof(done));
	q.push(HeapNode( 0,s ));
	while (!q.empty())
	{
		HeapNode x = q.top();
		q.pop();
		int u = x.u;
		if (done[u]) continue;
		done[u] = true;
		for (int i = 0; i<edges[u].size(); i++)
		{
			Edge& e = edges[u][i];
			if(d[e.to]>d[u]+e.dist)
            {
                d[e.to]=d[u]+e.dist;
                q.push(HeapNode(d[e.to],e.to));
            }
		}
	}
}
int main()
{
	    int mm;
	    int x;
	    while(scanf("%d%d%d",&n,&mm,&x)!=EOF)
		{
		    int a, b, c;
		    init();
		    int dd[1005];
		    int sum=-1;
		    for (int i = 0; i<mm; i++)
                {
                    scanf("%d %d %d", &a, &b, &c);
                    road[i].from=a,road[i].to=b,road[i].dist=c;
                    edges[a].push_back(Edge(b,c));
                }
            dijk(x);
            for(int i=1;i<=n;i++)
                dd[i]=d[i];
            init();//记得再次初始化
            for(int i=0;i<mm;i++)
            {
                a=road[i].from,b=road[i].to,c=road[i].dist;
                edges[b].push_back(Edge(a,c));
            }
            dijk(x);
            for(int i=1;i<=n;i++)
                {
                    dd[i]+=d[i];
                    if(dd[i]>sum)
                        sum=dd[i];
                }
            printf("%d\n",sum);
		}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值