Silver Cow Party POJ - 3268(Dijksta(两版),Spfa(两版))

Silver Cow Party POJ - 3268

Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 40513
Accepted: 18175

Describe

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 ≤ K ≤ 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 K
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.

题意

N个点每个点上有一头牛(不重要,记住有n个点就行) ,每个点之间的路线有M条单向路径(Ai到Bi点的距离是Ti),求哪个点去K点的最短距离加上K到这个点的最短距离的和最大是多少

思路

求出其他每个点K点的最短距离,以及K每个点最短距离(把路径翻一下a到b这条路改成b到a,利用Dijksta或者Spfa再求一遍最短路,就是各点到K点的最短路径),算出这两个最短距离,相加比大小就行。

题外话

这个没有负权值,Dijkstra或者Spfa都行。个人拿这道题总结一下,两个算法都写了,每个算法都写了两种表现形式,仅供参考!

类似题目

Invitation Cards POJ - 1511

Dijkstra 1.0版

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll long long
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}

const ll inf = 10000000000000005;

ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;

string s1, s2, s;

struct node1 {

	ll  end, len;

	node(ll x, ll y) :end(x), len(y) {}

};

struct node2 {

	ll begin, end, len;

}point[100020];

ll dis[5][100020], ismove[100020];

vector<vector<node1> >v;

void Dijkstra(ll k, ll f) {

	mes(dis[f], inf); mes(ismove, 0);

	dis[f][k] = 0;

	for (i = 0; i < n; i++) {

		ll now = 0, minn = inf;

		for (j = 1; j <= n; j++) {

			if (ismove[j] == 0 && dis[f][j] < minn) {

				now = j;

				minn = dis[f][j];

			}

		}

		ismove[now] = 1;

		for (j = 0, t = v[now].size(); j < t; j++) {

			if (ismove[v[now][j].end])continue;

			if (dis[f][v[now][j].end] > dis[f][now] + v[now][j].len) {

				dis[f][v[now][j].end] = dis[f][now] + v[now][j].len;

			}
			
		}
		
	}
	
}

int main() {

	n = read(); m = read(); k = read();
	
	for (i = 1, sum = 0, v.resize(n + 1); i <= m; i++) {

		point[i].begin = read(); point[i].end = read(); point[i].len = read();

		v[point[i].begin].push_back(node(point[i].end, point[i].len));

	}

	for (Dijkstra(k, 0), v.clear(), v.resize(n + 1), i = 1, sum = 0, v.resize(n + 1); i <= m; i++)
		v[point[i].end].push_back(node(point[i].begin, point[i].len));

	for (Dijkstra(k, 1),i = 2, j = dis[0][1] + dis[1][1]; i <= n; i++)
		j = max(j, dis[0][i] + dis[1][i]);

	cout << j << endl;

	return 0;
}

DIjkstra 2.0版

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll long long
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}
const ll inf = 10000000000000005;

ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;

string s1, s2, s;

struct node {

	ll  end, len;

	node(ll x, ll y) :end(x), len(y) {}

	friend bool operator < (node n1, node n2) {

		return n1.len > n2.len;

	}

};

struct node2 {

	ll begin, end, len;

}point[100020];

ll dis[5][100020], ismove[100020];

vector<vector<node> >v;

void Dijkstra(ll k, ll f) {

	mes(dis[f], inf); mes(ismove, 0);

	dis[f][k] = 0; priority_queue<node>que;

	que.push(node(k, 0));

	while (!que.empty()) {

		node n1 = que.top(); que.pop();

		if (ismove[n1.end])continue; ismove[n1.end] = 1;

		dis[f][n1.end] = n1.len;

		for (i = 0, j = v[n1.end].size(); i < j; i++) {

			node n2 = v[n1.end][i];

			if (ismove[n2.end])continue;

			n2.len += n1.len;

			que.push(n2);

		}

	}

}

int main() {

	n = read(); m = read(); k = read();

	for (i = 1, sum = 0, v.resize(n + 1); i <= m; i++) {

		cin >> point[i].begin >> point[i].end >> point[i].len;

		v[point[i].begin].push_back(node(point[i].end, point[i].len));

	}

	for (Dijkstra(k, 0), v.clear(), v.resize(n + 1), i = 1, sum = 0, v.resize(n + 1); i <= m; i++)
		v[point[i].end].push_back(node(point[i].begin, point[i].len));

	for (Dijkstra(k, 1), i = 2, j = dis[0][1] + dis[1][1]; i <= n; i++)
		j = max(j, dis[0][i] + dis[1][i]);

	cout << j << endl;

	return 0;
}



Spfa 1.0版

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll int
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}

const ll inf = 10000000000000005;

ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;

string s1, s2, s;

struct node1 {

	ll end, len;

	node(ll x, ll y) :end(x), len(y) {}

};
struct node2 {

	ll begin, end, len;

}point[100020];

vector<vector<node1> >v;

ll dis[5][1000005], ismove[1000005];

void Spfa(ll k, ll f) {

	mes(dis[f], inf); mes(ismove, 0);

	stack<ll>que; que.push(k); dis[f][k] = 0;

	while (!que.empty()) {

		t = que.top(); que.pop(); ismove[t] = 0;

		for (i = 0, j = v[t].size(); i < j; i++) {

			w = v[t][i].end;

			if (dis[f][w] > dis[f][t] + v[t][i].len) {

				dis[f][w] = dis[f][t] + v[t][i].len;

				if (ismove[w] == 0)	que.push(w);

				ismove[w] = 1;

			}

		}

	}

}
int main() {

	n = read(); m = read(); k = read();

	for (i = 1, v.resize(n + 1); i <= m; i++) {

		point[i].begin = read(); point[i].end = read(); point[i].len = read();

		v[point[i].begin].push_back(node(point[i].end, point[i].len));

	}
	
	for (Spfa(k, 0), v.clear(), v.resize(n + 1), i = 1; i <= m; i++)
		v[point[i].end].push_back(node(point[i].begin, point[i].len));

	for (Spfa(k, 1), i = 2, j = dis[0][1] + dis[1][1]; i <= n; i++)
		j = max(j, dis[0][i] + dis[1][i]);

	cout << j << endl;

	return 0;
}


Spfa 2.0版

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll long long
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define lp pair<ll, ll>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return x * f;
}

const ll inf = 10000000000000005;

ll n, m, i, j, k = 0, t, w, flag, x, y, z, sum;

string s1, s2, s;

struct node { ll begin, end, len, nextflag; }point[1000005];

ll head[1000005], dis[5][1000005], ismove[1000005];

void add_node(ll begin, ll end, ll len, ll nextflag) {

	point[nextflag].begin = begin;
	
	point[nextflag].end = end;
	
	point[nextflag].len = len;
	
	point[nextflag].nextflag = head[begin];
	
	head[begin] = nextflag;
	
}

void Spfa(ll k,ll f) {

	mes(dis[f], inf); mes(ismove, 0);
	
	stack<ll>que; que.push(k); dis[f][k] = 0;
	//用queue也行,但是博主自从在有一道题上被queue卡住时间用stack过了后
	//就不敢再用queue
	
	while (!que.empty()) {
	
		t = que.top(); que.pop(); ismove[t] = 0;
		
		for (i = head[t]; i != 0; i = point[i].nextflag) {
		
			w = point[i].end;
			
			if (dis[f][w] > dis[f][t] + point[i].len) {
			
				dis[f][w] = dis[f][t] + point[i].len;
				
				if (ismove[w] == 0)	que.push(w);
				
				ismove[w] = 1;
			}
			
		}
		
	}
	
}
int main() {

	n = read(); m = read(); k = read();
	
	for (i = 1, mes(head, 0), sum = 0; i <= m; i++) {
	
		point[i].begin = read(); point[i].end = read(); point[i].len = read();
		
		add_node(point[i].begin, point[i].end, point[i].len, i);
	}
	
	for (Spfa(k, 0),i = 1, mes(head, 0); i <= m; i++)
		add_node(point[i].end, point[i].begin, point[i].len, i);
		
	for (Spfa(k, 1),i = 2, j = dis[0][1] + dis[1][1]; i <= n; i++)	
		j = max(j, dis[0][i] + dis[1][i]);
		
	cout << j << endl;
	
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GUESSERR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值