POJ-3268-Silver Cow Party(最短路)

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: 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.

我的见解:两个方法8,下面注释挺详细的,求出最大的哈

dijkstra算法如下:

#pragma warning(disable:4996)
#define K(a)       scanf("%d", &a)
#define KK(a,b)    scanf("%d %d", &a, &b)
#define KKK(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define kd          printf
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;

const int INF = 9999999;
const int MAXX = 100005;            
struct edge { int to, cost; };
vector<edge>G[MAXX];
typedef pair<int, int >P;

int N,M,X;                              //点数,边数,终点位置
int d[MAXX], s[MAXX];

void dij(int s)         //注意是从0开始标号的,题目可能是1开始的
{
	priority_queue<P, vector<P>, greater<P>>que;       //从小到大
	fill(d, d + N, INF);
	d[s] = 0;
	que.push(P(0, s));

	while (!que.empty())
	{
		P p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first)continue;
		for (int i = 0; i < G[v].size(); i++)
		{
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main()
{
	KKK(N, M, X);
	int a, b, c;     //a通向b,cost c(权值)
	for (int i = 0; i < M; i++)
	{
		KKK(a, b, c);
		edge d;
		d.to = b-1, d.cost = c;
		G[a-1].push_back(d);        //往vector数组里面灌数据
	}
	dij(X-1);
	for (int i = 0; i < N; i++)
	{
		s[i] = d[i];
	}
	int res=0;
	for (int i = 0; i < N; i++)
	{
		dij(i);
		if (res < d[X - 1] + s[i])res = d[X - 1] + s[i];
	}
	cout << res << endl;
}

bellman-ford算法如下:

#pragma warning(disable:4996)
#define K(a)       scanf("%d", &a)
#define KK(a,b)    scanf("%d %d", &a, &b)
#define KKK(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define kd          printf
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;

const int MAXE = 100005;
const int MAXX = 1005;
const int INF = 9999999;
struct edge { int from, to, cost; };

edge es[MAXE];   //待定,最大边数+5

int V, E;  //顶点数,边数
int d[MAXX],k[MAXX];    //待定,最大点数+5

bool fuquan()       //判断负圈,这道题不用判断,题目给出权是大于0,因此用第一种方法更快!
{
	memset(d, 0, sizeof d);
	for (int i = 0; i < V; i++)
		for (int j = 0; j < E; j++)
		{
			edge e = es[j];
			if (d[e.to] > d[e.from] + e.cost)
			{
				d[e.to] = d[e.from] + e.cost;
				if (i == V - 1)return true;        //正常地while true最多V-1次,再多就是负圈,返回true

			}
		}
	return false;
}

void ford(int s)  //输入起点
{
	for (int i = 0; i < V; i++)d[i] = INF;
	d[s] = 0;
	while (true)
	{
		bool update = false;
		for (int i = 0; i < E; i++)
		{
			edge e = es[i];
			if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost)
			{
				d[e.to] = d[e.from] + e.cost;
				update = true;
			}
		}
		if (!update)break;
	}
}

int main()      //起点和边都是从0开始标志的!
{
	int X;
	KKK(V, E,X);
	int a, b, c;
	for (int i = 0; i < E; i++)
	{
		KKK(a, b, c);
		es[i].from = a-1;    //输入a到b和权值c
		es[i].to = b-1;
		es[i].cost = c;
	}
	ford(X-1);
	for (int i = 0; i < V; i++)k[i] = d[i];
	int res = 0;
	for (int i = 0; i < V; i++)
	{
		ford(i);
		if (res < k[i] + d[X - 1])res = k[i] + d[X - 1];

	}
	cout << res << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wujiekd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值