[Usaco07feb] Silver Bull Party Cow

Source: Internet
Author: User
Tags gcd

Title Description

Winter vacation arrived, N cattle are going to a party numbered X (1≤x≤n) of the farm held by the parties (1≤n≤1000), between the farm there is M (1≤m≤100000) Strips of road, each road long Ti (1≤ti≤100).

Every cow must go home after the party, whether it is to go to a party or go home, each cow will choose the shortest path, the shortest path of the N cattle (a round trip) the longest path length.

Input/output format

Input format:

First row of three integers n,m, X;

The second line to line m+1: There are three integers per line Ai,bi, TI, indicates that there is a road from Ai Farm to Bi Farm, the length of ti.

Output format:

An integer that represents the longest shortest short length.

Input/Output sample

Input Sample # #:

4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3

Sample # # of output:

10
Exercises

It's really a water problem.

As long as the shortest way to build a positive, reverse map can be

Code:

#include <bits/stdc++.h> #pragma GCC optimize (3) namespace zdy{#define RES Register #define RI Res int #defin e ll long long #define DB double #define SHT short #define IL inline #define MB template <class t> #d Efine Fur (i,x,y) for (RI i=x;i<=y;i++) #define FUR (I,x,y) for (i=x;i<=y;i++) #define FDR (i,x,y) for (RI i=x;i>    =y;i--) #define IN2 (x, y) in (x), in (y) #define in3 (Z, z) in2 (x, y) #define IN4 (a,b,c,d) in2 (A, b); in2 (c,d) #define OUTN (x) out (x), PC (' \ n ') #define CLR (x, y) memset (x,y,sizeof) #define CPY (x, y) memcpy (x,y,sizeof) #de Fine FL (i,x) for (RI i=head[x],to;to=e[i].to,i;i=e[i].nxt) #define INF 2147483630 #define FIN (s) freopen (S ".", "R", s Tdin) #define FOUT (s) freopen (S ". Out", "w", stdin) #define GT IO.GC () #define L2 (N) (log (n)/log (2)) MB il T ABS (    T x) {return x>0?x:-x;}    MB il t MAX (t x,t y) {return x>y?x:y;}    MB il t MIN (t x,t y) {return x<y?x:y;} MB il t GCD (t x,t y) {return y?    GCD (y,x%y): x;} MB il void SWAP (t x,t y) {T t=x;y=t;x=y;}} Using namespace zdy;using namespace Std;class io{#define FOK (ch!=eof) #define SEP (ch== "| | ch== ' \ n ' | | ch== ' \ t ') #define DSEP!isdigit (CH) #define NEQ (A, a) ((a)-(b) >1e-6) Char Rbuf[1<<20],wbuf[1<<20],b,*p   1,*P2;   int rs,ws,s;       Public:io ():p 1 (rbuf), p2 (wbuf), S (1000000), RS (1000000), WS ( -1), B (1) {} ~io () {fwrite (wbuf,1,ws+1,stdout);} Il char GC () {return rs==s&& (P1=rbuf,rs=-1, (S=fread (Rbuf,1,s+1,stdin)-1) ==-1)? (       b=0,eof):(++rs,*p1++);}       Il void pc (int x) {ws==1000000&& (P2=wbuf,ws=-1,fwrite (wbuf,1,1000001,stdout)), ++ws,*p2++=x;} Il void puts (const char str[]) {fwrite (wbuf,1,ws+1,stdout)? (       Ws=-1): 0,fwrite (Str,1,strlen (str), stdout);}       Il void gl (string& s) {for (res char ch; (CH=GC ())! = ' \ n ' &&fok;) s+=ch;} Il io& operator>> (int& x) {x=0;res char f=0,ch=gc (), while (Dsep&&fok) f|= (ch== '-'), CH=GC (); (IsDigit (CH)) x= (x<<1) + (x< <3) + (ch^48), CH=GC (); return x=f?-x:x,*this;} Il io& operator>> (ll& x) {x=0;res char f=0,ch=gc (), while (Dsep&&fok) f|= (ch== '-'), CH=GC ();       IsDigit (CH)) x= (x<<1) + (x<<3) + (ch^48), CH=GC (); return x=f?-x:x,*this;}       Il io& operator>> (char& ch) {return CH=GC (), *this;} Il io& operator>> (string& s) {res char CH=GC (), while (Sep&&fok) CH=GC (), while (!sep&&fok)       S+=CH,CH=GC (); return *this;} Il io& operator>> (double& x) {x=0;res char f=0,ch=gc ();d ouble d=0.1;while (Dsep&&fok) f|= (ch== '-' ), CH=GC (), while (IsDigit (CH)) x=x*10+ (ch^48), CH=GC (); if (ch== '. ')       while (IsDigit (CH=GC ())) x+=d* (ch^48), D*=0.1;return x=f?-x:x,*this;} Il io& operator<< (int x) {res char ch[10],i=-1;! X? (PC (' 0 '), 0): 0,x<0?       (PC ('-'), x=-x): 0;while (x) ch[++i]=x%10+48,x/=10;while (~i) PC (Ch[i]),--I;return *this;} Il io& operator<< (ll x) {res char ch[20],i=-1;! X? (PC (' 0 '), 0): 0,x<0? (PC ('-'), x=-x): 0;while (x) ch[++i]=x%10+48,x/=10;while (~i) PC (Ch[i]),--I;return *this;}       Il io& operator<< (char ch) {return PC (CH), *this;}       Il io& operator<< (char str[]) {return puts (str), *this;} Il io& operator<< (double x) {int y=int (x); *this<<y;x-=y;while (NEQ (X,int (x))) x*=10;x?*this<< '. '       <<int (x): 0;return *this;} Il operator bool () {return b;}} io; #define N 1001int n,m,s,cnt,cnt,d[n],d[n],head[n],head[n];bool v[n];struct edge{int to,w,nxt;} E[100001],E[100001]; #define FL (i,x) for (ri i=head[x],to;to=e[i].to,i;i=e[i].nxt) struct cmp{bool operator () (int a,int b) {return d[a]>d[b];}}; Priority_queue<int,vector<int>,cmp>q;il void Add (int x,int y,int W) {E[++CNT].TO=Y;E[CNT].W=W;E[CNT].NXT =head[x];head[x]=cnt;} Il void ADD (int x,int y,int W) {e[++cnt].to=y; E[cnt].w=w; E[CNT].NXT=HEAD[X]; head[x]=cnt;}    Il void Spfa () {clr (d,126); CLR (d,126);    D[s]=d[s]=0;q.push (s);    int x;        while (!q.empty ()) {v[x=q.top ()]=0;q.pop ();   FL (i,x)     if (D[x]+e[i].w<d[to]) {D[TO]=D[X]+E[I].W;        if (!v[to]) Q.push (to), v[to]=1;            } FL (I,x) if (D[x]+e[i].w<d[to]) {D[TO]=D[X]+E[I].W;        if (!v[to]) Q.push (to), v[to]=1;    }}}int Main () {io>>n>>m>>s;    int x,y,w,ans=0;    Fur (i,1,m) Io>>x>>y>>w,add (x,y,w), add (Y,X,W);    SPFA ();    Fur (i,1,n) Ans=max (Ans,d[i]+d[i]); io<<ans<< ' \ n ';}
Eggs:

Double experience: Luogu P1629 postman Messenger

(change all the maximum values to all the sums.)

luogu-judger-enable-o2#include<bits/stdc++.h> #pragma GCC optimize (3) namespace zdy{#define RES Register #de Fine ri Res int #define LL Long long #define DB double #define SHT short #define IL inline #define MB Templ Ate <class t> #define FUR (i,x,y) for (RI i=x;i<=y;i++) #define FUR (I,x,y) for (i=x;i<=y;i++) #define Fd R (i,x,y) for (RI i=x;i>=y;i--) #define IN2 (x, Y) in (×), in (#define) #define IN3 (x, z) in2 (z) in4 (a,b,c , d) in2 (A, B), in2 (c,d) #define OUTN (x) out (x), PC (' \ n ') #define CLR (x, y) memset (x,y,sizeof (×)) #define CPY (y) mem CPY (x,y,sizeof (x)) #define FL (i,x) for (RI i=head[x],to;to=e[i].to,i;i=e[i].nxt) #define INF 2147483630 #define FI N (s) freopen (S ".", "R", stdin) #define FOUT (s) freopen (S ". Out", "w", stdin) #define GT IO.GC () #define L2 (N) (log (n    )/log (2)) MB il T ABS (t x) {return x>0?x:-x;}    MB il t MAX (t x,t y) {return x>y?x:y;}    MB il t MIN (t x,t y) {return x<y?x:y;} MB IL T GCD (t x,t y) {return y?    GCD (y,x%y): x;} MB il void SWAP (t x,t y) {T t=x;y=t;x=y;}} Using namespace zdy;using namespace Std;class io{#define FOK (ch!=eof) #define SEP (ch== "| | ch== ' \ n ' | | ch== ' \ t ') #define DSEP!isdigit (CH) #define NEQ (A, a) ((a)-(b) >1e-6) Char Rbuf[1<<20],wbuf[1<<20],b,*p   1,*P2;   int rs,ws,s;       Public:io ():p 1 (rbuf), p2 (wbuf), S (1000000), RS (1000000), WS ( -1), B (1) {} ~io () {fwrite (wbuf,1,ws+1,stdout);} Il char GC () {return rs==s&& (P1=rbuf,rs=-1, (S=fread (Rbuf,1,s+1,stdin)-1) ==-1)? (       b=0,eof):(++rs,*p1++);}       Il void pc (int x) {ws==1000000&& (P2=wbuf,ws=-1,fwrite (wbuf,1,1000001,stdout)), ++ws,*p2++=x;} Il void puts (const char str[]) {fwrite (wbuf,1,ws+1,stdout)? (       Ws=-1): 0,fwrite (Str,1,strlen (str), stdout);}       Il void gl (string& s) {for (res char ch; (CH=GC ())! = ' \ n ' &&fok;) s+=ch;} Il io& operator>> (int& x) {x=0;res char f=0,ch=gc (), while (Dsep&&fok) f|= (ch== '-'), CH=GC (); (IsDigit(CH))       x= (x<<1) + (x<<3) + (ch^48), CH=GC (); return x=f?-x:x,*this;} Il io& operator>> (ll& x) {x=0;res char f=0,ch=gc (), while (Dsep&&fok) f|= (ch== '-'), CH=GC ();       IsDigit (CH)) x= (x<<1) + (x<<3) + (ch^48), CH=GC (); return x=f?-x:x,*this;}       Il io& operator>> (char& ch) {return CH=GC (), *this;} Il io& operator>> (string& s) {res char CH=GC (), while (Sep&&fok) CH=GC (), while (!sep&&fok)       S+=CH,CH=GC (); return *this;} Il io& operator>> (double& x) {x=0;res char f=0,ch=gc ();d ouble d=0.1;while (Dsep&&fok) f|= (ch== '-' ), CH=GC (), while (IsDigit (CH)) x=x*10+ (ch^48), CH=GC (); if (ch== '. ')       while (IsDigit (CH=GC ())) x+=d* (ch^48), D*=0.1;return x=f?-x:x,*this;} Il io& operator<< (int x) {res char ch[10],i=-1;! X? (PC (' 0 '), 0): 0,x<0?       (PC ('-'), x=-x): 0;while (x) ch[++i]=x%10+48,x/=10;while (~i) PC (Ch[i]),--I;return *this;} Il io& operator<< (ll x) {res char ch[20],i=-1;! X? (PC (' 0 '), 0):0,x<0?       (PC ('-'), x=-x): 0;while (x) ch[++i]=x%10+48,x/=10;while (~i) PC (Ch[i]),--I;return *this;}       Il io& operator<< (char ch) {return PC (CH), *this;}       Il io& operator<< (char str[]) {return puts (str), *this;} Il io& operator<< (double x) {int y=int (x); *this<<y;x-=y;while (NEQ (X,int (x))) x*=10;x?*this<< '. '       <<int (x): 0;return *this;} Il operator bool () {return b;}} io; #define N 1001int n,m,cnt,cnt,d[n],d[n],head[n],head[n];bool v[n];struct edge{int to,w,nxt;} E[100001],E[100001]; #define FL (i,x) for (ri i=head[x],to;to=e[i].to,i;i=e[i].nxt) struct cmp{bool operator () (int a,int b) {return d[a]>d[b];}}; Priority_queue<int,vector<int>,cmp>q;il void Add (int x,int y,int W) {E[++CNT].TO=Y;E[CNT].W=W;E[CNT].NXT =head[x];head[x]=cnt;} Il void ADD (int x,int y,int W) {e[++cnt].to=y; E[cnt].w=w; E[CNT].NXT=HEAD[X]; head[x]=cnt;}    Il void Spfa () {clr (d,126); CLR (d,126);    D[1]=d[1]=0;q.push (1);    int x; while (!q.empty ()) {v[x=q.top ()]=0;Q.POP ();            FL (i,x) if (D[x]+e[i].w<d[to]) {D[TO]=D[X]+E[I].W;        if (!v[to]) Q.push (to), v[to]=1;            } FL (I,x) if (D[x]+e[i].w<d[to]) {D[TO]=D[X]+E[I].W;        if (!v[to]) Q.push (to), v[to]=1;    }}}int Main () {io>>n>>m;    int x,y,w,ans=0;    Fur (i,1,m) Io>>x>>y>>w,add (x,y,w), add (Y,X,W);    SPFA ();    Fur (I,2,n) ans+=d[i]+d[i]; io<<ans<< ' \ n ';}

[Usaco07feb] Silver Bull Party Cow Parties

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.