"Bzoj" 1631: [Usaco2007 Feb]cow Party (Dijskstra)

Source: Internet
Author: User

http://www.lydsy.com/JudgeOnline/problem.php?id=1631

See m<=100000 decisively with SPFA (but like Dij than SPFA is still slower here?) )

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue>using namespace std; #define REP (i, n) for (int i=0; i< (n); ++i) # Define FOR1 (I,a,n) for (int i= (a), i<= (n), ++i) #define FOR2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (I,a,n) for ( int i= (a); i>= (n); i) #define FOR4 (i,a,n) for (int i= (a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define Read (a) a=getint () #define PRINT (a) printf ("%d", a) #define DBG (x) cout << #x << "=" << x << endl# Define Printarr (A, N, m) Rep (AAA, N) {rep (BBB, m) cout << a[aaa][bbb]; cout << Endl;} inline const int Getint () {int r=0, k=1; char C=getchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }inline const int MAX (const int &a, const int &b) {return a>b?a:b;} inline const int min (const int &a, const int &b) {return a<b?a:b;} const int n=1005, m=100005, Oo=~0u>>2;int Ihead[n], N, M, D[n], T, CNT, d1[n], x[m], y[m], w[m];struct ED {int to, Next, W; }e[m];struct nd {int id; const BOOL operator< (const ND &b) const {return d[id]>d[b.id];}}; priority_queue<nd> q;void Add (int u, int v, int w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;} void Dij (int s) {For1 (i, 0, N) d[i]=oo;d[s]=0; ND t={s};int u, V;q.push (t), while (Q.size ()) {U=q.top (). ID; q.pop (); for (int i=ihead[u]; i; i=e[i].next) if (d[v=e[i].to]& GT;D[U]+E[I].W) {D[v]=d[u]+e[i].w;t.id=v;q.push (t);}}} int main () {read (n); read (m); Read (T); int u, V, w;rep (i, m) {read (u); Read (v); Read (w); X[i]=u; Y[i]=v; W[i]=w;add (V, U, W);} int Mx=0;dij (T); For1 (i, 1, n) d1[i]=d[i];cnt=0; CC (ihead, 0); Rep (I, M) add (X[i], y[i], w[i]);d ij (T); For1 (i, 1, n) {mx=max (MX, d1[i]+d[i]);} Print (MX); return 0;}

Description Farm has N (1≤n≤1000) cattle stalls, each of which has 1 cows to participate in the X Cow cattle party. A total of M (1≤m≤100000) A one-way link to the barn, article I crippled need ti time to pass. Cows are lazy, so whether they go to the X-Barn party or return to their home, they use the least-used route. So how much time does it take to go back and forth with the biggest cows? Input

Line 1th: Three integers separated by a space.

Line 2nd to line m+1, three integers separated by spaces: Ai, Bi, and Ti. Represents the starting point of a road, the end point, and the time it takes.

Output

Unique line: An integer: The maximum amount of time that will be spent in all cows participating in the party.

Sample INPUT4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output10
HINT

Sample Description:


There were 4 cows at the party, 8 Roads and a party on the 2nd farm.


The 4th cow can go directly to the party location (spending 3 time), then the return route passes through the 1th and 3rd farms (7 time), for a total of 10 hours.

Source

Silver

"Bzoj" 1631: [Usaco2007 Feb]cow Party (Dijskstra)

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.