POJ - 3268 Silver Cow Party
作者:互联网
POJ - 3268 题目链接
一道最短路问题,但是和单源对短路又不一样,这里有来回的问题
首先每头牛回的路径很简单,就是以终点为起始点来通过一个dis数组来记录他们回到原粗的距离,
因为这里是单向边,每头牛到终点的距离肯定和回到起始点的距离不一样,
这里我们考虑反向建边,又再一次把问题变成的从终点往回走的问题了,
这样我们统计两个dis数组的值就行了。
//Powered by CK
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N1 = 1e3 + 10, N2 = 1e5 + 10;
int head1[N1], to1[N2], nex1[N2], value1[N2], visit1[N1], dis1[N1], cnt1 = 1;
int head2[N1], to2[N2], nex2[N2], value2[N2], visit2[N1], dis2[N2], cnt2 = 1;
int n, m, s;
struct cmp {
bool operator () (const PII & a, const PII & b) const {
return a.second > b.second;
}
};
void add1(int x, int y, int w) {
to1[cnt1] = y;
value1[cnt1] = w;
nex1[cnt1] = head1[x];
head1[x] = cnt1++;
}
void add2(int x, int y, int w) {
to2[cnt2] = y;
value2[cnt2] = w;
nex2[cnt2] = head2[x];
head2[x] = cnt2++;
}
void Dijkstra1() {
priority_queue<PII, vector<PII>, cmp> q;
for(int i = 1; i <= n; i++) dis1[i] = INF;
dis1[s] = 0;
q.push(make_pair(s, 0));
while(!q.empty()) {
int temp = q.top().first;
q.pop();
if(visit1[temp]) continue;
visit1[temp] = 1;
for(int i = head1[temp]; i; i = nex1[i])
if(dis1[to1[i]] > dis1[temp] + value1[i]) {
dis1[to1[i]] = dis1[temp] + value1[i];
q.push(make_pair(to1[i], dis1[to1[i]]));
}
}
}
void Dijkstra2() {
priority_queue<PII, vector<PII>, cmp> q;
for(int i = 1; i <= n; i++) dis2[i] = INF;
dis2[s] = 0;
q.push(make_pair(s, 0));
while(!q.empty()) {
int temp = q.top().first;
q.pop();
if(visit2[temp]) continue;
visit2[temp] = 1;
for(int i = head2[temp]; i; i = nex2[i])
if(dis2[to2[i]] > dis2[temp] + value2[i]) {
dis2[to2[i]] = dis2[temp] + value2[i];
q.push(make_pair(to2[i], dis2[to2[i]]));
}
}
}
int main() {
// freopen("in.txt", "r", stdin);
int x, y, w;
while(scanf("%d %d %d", &n, &m, &s) != EOF) {
memset(visit1, 0, sizeof visit1);
memset(visit2, 0, sizeof visit2);
memset(head1, 0, sizeof head1);
memset(head2, 0, sizeof head2);
cnt1 = cnt2 = 1;
for(int i = 0; i < m; i++) {
scanf("%d %d %d", &x, &y, &w);
add1(x, y, w);
add2(y, x, w);
}
Dijkstra1();
Dijkstra2();
int ans = 0;
for(int i = 1; i <= n; i++)
ans = max(ans, dis1[i] + dis2[i]);
printf("%d\n", ans);
}
return 0;
}
标签:const,int,POJ,cnt2,cnt1,N1,3268,N2,Silver 来源: https://www.cnblogs.com/lifehappy/p/12608244.html