【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

时间:2023-03-09 16:22:14
【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

题目链接:https://www.luogu.org/problemnew/show/P1821

反向多存一个图,暴力跑两遍

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = ;
const int inf = 0x7fffffff;
int n, m, x, dis1[maxn], dis2[maxn];
bool vis1[maxn] = {}, vis2[maxn] = {};
struct edge{
int len, to, next;
}e1[maxn], e2[maxn];
int head1[maxn], head2[maxn], cnt1, cnt2;
void SPFA1()
{
queue<int> q1;
vis1[x] = ;
dis1[x] = ;
q1.push(x);
while(!q1.empty())
{
int now1 = q1.front();
q1.pop();
vis1[now1] = ;
for(int i = head1[now1]; i!= ; i = e1[i].next)
{
if(dis1[e1[i].to] > dis1[now1]+e1[i].len)
{
dis1[e1[i].to] = dis1[now1]+e1[i].len;
if(!vis1[e1[i].to])
{
vis1[e1[i].to] = ;
q1.push(e1[i].to);
}
}
}
}
}
void SPFA2()
{
queue<int> q2;
vis2[x] = ;
dis2[x] = ;
q2.push(x);
while(!q2.empty())
{
int now2 = q2.front();
q2.pop();
vis2[now2] = ;
for(int i = head2[now2]; i ; i = e2[i].next)
{
if(dis2[e2[i].to] > dis2[now2]+e2[i].len)
{
dis2[e2[i].to] = dis2[now2]+e2[i].len;
if(!vis2[e2[i].to])
{
vis2[e2[i].to] = ;
q2.push(e2[i].to);
}
}
}
}
}
int main()
{
cin>>n>>m>>x;
for(int i = ; i <= n; i++)
{
dis2[i] = inf;
dis1[i] = inf;
}
for(int i = ; i <= m; i++)
{
int u,v,w;
cin>>u>>v>>w;
e1[i].to = v;
e1[i].len = w;
e1[i].next = head1[u];
head1[u] = i; e2[i].to = u;
e2[i].len = w;
e2[i].next = head2[v];
head2[v] = i;
}
SPFA1();
SPFA2();
int ans = ;
for(int i = ; i <= n; i++)
{
ans = max(dis1[i]+dis2[i],ans);
}
cout<<ans;
return ;
}

Ctrl+C Ctrl+V 真毒瘤,弄得我12分反好几次,真是老年OI选手