POJ 3268 Silver Cow party SPFA.
Free to have nothing to do with water a cow problem sleep well. It's going to be 11 right now. "Tears
Description:n (1 <= n <=), m stripe weight (cost) has a forward edge (1 <= m <= 1e5), given a target point T, to find the value of the maximum value.
We define the value of each point. The value of a point = the minimum cost of the current point to the target point and the minimum cost of the target point to the current point.
Solution:
Well.. The solution is simple. Run SPFA on T and turn the side upside down SPFA.
Why do you do this? " Although obviously. But in order to get the logical level of the problem analysis, write it. 】
We consider the value of each point. Let's consider the minimum cost of the target point to the current point. This is easy to ask. SPFA to T run once.
Value of point = Minimum cost of the target point to the current point * 2?
Wrong. There is a direction graph. Unidirectional.
The simple SPFA to find the answer, a legal side is so defined, you can reach V, we can expand.
The reason we get the answer is that you can reach V when V does not necessarily reach U. Let's change the condition of the SPFA extension node: if V can reach U, it is considered as having an edge between U and V.
So we SPFA the side reversal. Then find out the maximum value of reverse_dis[i] + dis[i] just fine.
Slag Code:
1#include <iostream>2#include <string.h>3#include <stdio.h>4#include <algorithm>5#include <math.h>6#include <stdlib.h>7 using namespacestd;8 Const intM = 1e5 +5;9 Const intN = ++5;Ten structqwq{intV, Val, next;} EDGE[M], revedge[m]; One BOOLVis[n]; A intCNT =0, revcnt =0, N, M, T; - intQ[n *N], head[n], revhead[n], dis[n], revdis[n]; - voidAddintUuintvvintTT) the { -edge[++ cnt] = (QWQ) {VV, TT, Head[uu]}; HEAD[UU] =CNT; -revedge[++ revcnt] = (QWQ) {UU, TT, REVHEAD[VV]}; REVHEAD[VV] =revcnt; - } + voidsolve () { -memset (Vis,0,sizeof(Vis)); +memset (Q,0,sizeof(q)); Amemset (DIS,0x3f,sizeof(DIS)); at //Back route, normal -VIS[T] =1; - inth =0, t =0; -q[++ T] =T; -DIS[T] =0; - while(H <t) { in intUU = q[++ h]; VIS[UU] =0; - for(inti = Head[uu]; ~i; i =Edge[i].next) { to intv =edge[i].v; + if(Dis[v] > Dis[uu] + edge[i].val) {//Relax First -DIS[V] = Dis[uu] +Edge[i].val; the if(!Vis[v]) { *VIS[V] =1; q[++ T] =v; $ }Panax Notoginseng } - } the } +memset (Vis,0,sizeof(Vis)); Amemset (Q,0,sizeof(q)); thememset (Revdis,0x3f,sizeof(Revdis)); + //go route, reverse mode Qaq -VIS[T] =1; $h =0, t =0; $q[++ T] =T; -REVDIS[T] =0; - while(H <t) { the intUU = q[++ h]; VIS[UU] =0; - for(inti = Revhead[uu]; ~i; i =Revedge[i].next) {Wuyi intv =revedge[i].v; the if(Revdis[v] > Revdis[uu] + revedge[i].val) {//Relax Qaq First . -REVDIS[V] = Revdis[uu] +Revedge[i].val; Wu if(!Vis[v]) { -VIS[V] =1; q[++ T] =v; About } $ } - } - } - intMX =0; A for(inti =1; I <= N; i + +){ + //printf ("%d%d\n", Dis[i], revdis[i]); the if(I! = T && dis[i] + revdis[i] > mx) mx = dis[i] +Revdis[i]; - } $printf"%d\n", MX); the } the intMain () the { theMemset (Head,-1,sizeof(head)); -memset (Revhead,-1,sizeof(Revhead)); inscanf"%d%d%d", &n, &m, &T); the for(inti =1; I <= m; i + +){ the intUU, VV, TT; Aboutscanf"%d%d%d", &uu, &VV, &TT); the Add (UU, VV, TT); the } the solve (); + return 0; -}
POJ 3268 Silver Cow Party
POJ 3268 Silver Cow Party SPFA