例题:奶牛派对
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int maxm = maxn * maxn;
const int inf = 0x7f7f7f7f;
int n = 0, m = 0, a;
int h[maxn] = {}, to[maxm] = {}, nxt[maxm] = {}, w[maxm] = {}, tot = 0;
int dis[maxn] = {}, ans;
bool vis[maxn] = {};
queue<int> q;
void addedge(int x, int y, int z)
{
to[++tot] = y;
nxt[tot] = h[x];
w[tot] = z;
h[x] = tot;
}
int spfa(int x, int end)
{
memset(dis, 0x3f, sizeof(dis));
dis[x] = 0;
q.push(x);
while(q.size())
{
x = q.front();
q.pop();
vis[x] = false;
for(int i=h[x]; i; i=nxt[i])
{
int y = to[i];
if(dis[y] > dis[x] + w[i])
{
dis[y] = dis[x] + w[i];
if(!vis[y]) q.push(y);
}
}
}
return dis[end];
}
int main()
{
int x = 0, y = 0, z = 0;
scanf("%d%d%d", &n, &m, &a);
for(int i=1; i<=m; i++)
{
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
}
for (int i = 1; i <= n; i++)
{
int sum = spfa(i,a)+spfa(a,i);
ans = max(ans,sum);
}
cout << ans << endl;
return 0;
}