P1821 [USACO07FEB]银牛派对Silver Cow Party
题目描述
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?
寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。
每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。
输入输出格式
输入格式:
第一行三个整数N,M, X;
第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。
输出格式:
一个整数,表示最长的最短路得长度。
输入输出样例
说明
神奇的反向存图操作
为了不写两遍spfa
可用x<<1,x<<1|1 这种方式将一个点拆成两个点存
这样正着走反着走都可以啦
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 2147483647 const ll INF = 0x3f3f3f3f3f3f3f3fll; #define ri register int template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } #define scanf1(x) scanf("%d", &x) #define scanf2(x, y) scanf("%d%d", &x, &y) #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X) #define pi acos(-1) #define me(x, y) memset(x, y, sizeof(x)); #define For(i, a, b) for (int i = a; i <= b; i++) #define FFor(i, a, b) for (int i = a; i >= b; i--) #define bug printf("***********\n"); #define mp make_pair #define pb push_back const int N = 4e5+5; // name******************************* int n,m,x; int a,b,t; struct edge { int to,next,w; } e[N]; int tot=0; int Head[N]; int vis[N]; queue<int>que; int dis[N]; int ans=0; // function****************************** void add(int u,int v,int w) { e[++tot].to=v; e[tot].next=Head[u]; Head[u]=tot; e[tot].w=w; } void spfa(int x) { que.push(x); vis[x]=1; dis[x]=0; while(!que.empty()) { int u=que.front(); vis[u]=0; que.pop(); for(int p=Head[u]; p; p=e[p].next) { int v=e[p].to; int w=e[p].w; if(dis[v]>dis[u]+w) { dis[v]=dis[u]+w; if(!vis[v]) { vis[v]=1; que.push(v); } } } } } //*************************************** int main() { // ios::sync_with_stdio(0); // cin.tie(0); // freopen("test.txt", "r", stdin); // freopen("outout.txt","w",stdout); cin>>n>>m>>x; me(dis,127); For(i,1,m) { scanf("%d%d%d",&a,&b,&t); add(a<<1,b<<1,t); add(b<<1|1,a<<1|1,t); } spfa(x<<1); spfa(x<<1|1); For(i,1,n) { ans=max(ans,dis[i<<1]+dis[i<<1|1]); } cout<<ans; return 0; }