Rokua 1821 [Usaco07feb] Silver Bull Party The shortest path to the silvery Cow parties

Source: Internet
Author: User

Topic Portal

The daily water wave, is to the positive diagram run over SPFA, and then build a reverse graph, that is, all the other nodes to the source point of distance, and then run again SPFA.

The final output is two times the maximum value of the distance is OK.

Attach the AC code:

#include <cstdio> #include <cctype> #include <queue> #include <cstring> #define M 100010 #define N

1010 using namespace Std; struct note{int to,w,nt;}
Z[M*2],F[M*2];
int n,m,st,num,hz[n],hf[n],x,y,w,d1[n],d2[n];
Long long ans;
Queue <int> que;

BOOL B[n];
	void Read (int& a) {static char C=getchar (); A=0;int f=1;
	while (!isdigit (c)) {if (c== '-') F=-1;c=getchar ();}
	while (IsDigit (c)) a=a*10+c-' 0 ', C=getchar ();
A*=f;return;
	} void Add (int x,int y,int W) {z[num]= (note) {X,w,hz[y]};
	f[num]= (note) {y,w,hf[x]};
hz[y]=hf[x]=num++;
	} void Spfa1 () {while (!que.empty ())) Que.pop ();
	memset (b,0,sizeof b);
	memset (d1,0x3f,sizeof D1);
	Que.push (ST), d1[st]=0,b[st]=1;
		while (!que.empty ()) {int P=que.front (); Que.pop (), b[p]=0;
				for (int i=hz[p]; ~i; i=z[i].nt) if (D1[Z[I].TO]&GT;D1[P]+Z[I].W) {D1[Z[I].TO]=D1[P]+Z[I].W;
					if (!b[z[i].to]) {b[z[i].to]=1;
				Que.push (z[i].to);
}}} return; } void Spfa2 () {while (!que.empty ()) Que.pop ();
	memset (b,0,sizeof b);
	memset (d2,0x3f,sizeof D2);
	Que.push (ST), d2[st]=0,b[st]=1;
		while (!que.empty ()) {int P=que.front (); Que.pop (), b[p]=0;
				for (int i=hf[p]; ~i; i=f[i].nt) if (D2[F[I].TO]&GT;D2[P]+F[I].W) {D2[F[I].TO]=D2[P]+F[I].W;
					if (!b[f[i].to]) {b[f[i].to]=1;
				Que.push (f[i].to);
}}} return;
	} int main (void) {read (n), read (m), read (ST);
	memset (hz,-1,sizeof Hz), memset (hf,-1,sizeof HF);
	for (int i=1; i<=m; ++i) Read (x), read (y), read (W), add (X,Y,W);
	SPFA1 (), SPFA2 ();
	for (int i=1; i<=n; ++i) Ans=max (ans, (long Long) d1[i]+d2[i]);
	printf ("%lld", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.