JSOI2007 Summer Camp Exam Tour

Source: Internet
Author: User
Tags cmath

Title Description

Successfully passed the test of the yellow pharmacist, the following can enjoy a tour of Peach Island!

You have to start from the Xi ' an island to the east, and then leave at the pier in Tung Tau. But when you play once, found the peach Blossom Island scenery is really very beautiful!!! So you also want to take a boat from the pier in the east of Peach Blossom Island back to the XI ' an, and then play again, but Peach Island has a rule: you can visit countless times, but the route of each play can not be exactly the same.

We have abstracted the Peach Island into a graph, a total of n points representing the intersection of the road, M-side represents the road, The Edge is directed (only in accordance with the direction of the side of the walk), and may have connected to the same two points of the edge. The input guarantees that the graph has no rings and that there is at least one route from the XI Tau to the east. The two routes are considered to be different when and only if the path they pass is not exactly the same.

Your task is: how long will it take to get all the different routes done?

Input/output format

Input format:

The 1th act is 5 integers: N, m, S, T, t0, respectively, number of points, number of sides, numbers of the island's tau, the number of the island East (numbered from 1 to N) and the time you sailed from the island's Tung Tau to the XI ' an.

The following M-line, 3 integers per line: x, y, T, indicates a path from point x to Y with a walking time of T.

Multiple data for each row is separated by a space, and:2<=n<=10000; 1<=m<=50000;t<=10000;t0<=10000

Output format:

Assuming the total is time-consuming, output the value of all mod 10000 (totals to 10000).

Input and Output Sample input example # #:
3 4 1 3 71 2 52 3 72 3 101 3 15
Sample # # of output:
56
Description

"Sample description"

A total of 3 routes can be from point 1 to 3, respectively, 1-2-3,1-2-3,1-3.

The time is calculated as:

(5+7) +7 + (5+10) +7 + (15) =56

Solution:
Violence:
Search all the routes, 40 points.


Positive Solutions: addition principle + multiplication principle


set Sum[i] is the number of scenarios from the starting point to the I,
len[i" is the sum of the paths to I from the starting point
Consider an edge I-j, The time spent is T
Then there are:
len[j]=len[j]+len[i]+t*sum[i";
sum[j]=sum[j]+sum[i]
for the starting point s:sum[s]=1;

Pay attention to the nature of the topic:
"Input guarantees that the graph has no ring, and there is at least one route from the XI Tau to the east."
So we can use topological sort to do this work and get
Sum[i] and Len[i].
We can also build the opposite side, starting from T to find, DFS,
Thus getting sum[i] and Len[i]

Code:

1. Topological sequencing practices

1#include <algorithm>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <string>6#include <queue>7#include <cmath>8 #definell Long Long9 #defineDB DoubleTen #defineMoD 10000 One #defineEPS 1e-3 A #defineINF 2147483600 - using namespacestd; -InlineintRead () the { -     intx=0, w=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') w=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') x= (x<<3) + (x<<1) +ch-'0', ch=GetChar (); +     returnx*W; - } + Const intn=1e6+Ten; A structnode{ at     intU,v,c,ne; - }e[n]; - intH[n],tot,v[n],d[n]; - voidAddintUintVintc) - { -d[v]++; inTot++;e[tot]= (node) {u,v,c,h[u]};h[u]=tot; - } to intN,m,s,t,t0,sum[n],len[n]; +queue<int>Q; - voidTuopu () the { *sum[s]=1; $      for(intI=1; i<=n;++i)Panax Notoginseng      if(d[i]==0) Q.push (i); -      while(!q.empty ()) the     { +         intHfSQ.front (); Q.pop (); A          for(intI=h[ff];i;i=e[i].ne) the         { +             intRr=e[i].v; -d[rr]--; $Sum[rr]= (Sum[rr]+sum[ff])%MoD; $len[rr]= (len[rr]+len[ff]+e[i].c*sum[ff]%mod)%MoD; -             if(d[rr]==0) Q.push (RR); -         } the     } - }Wuyi intMain () the { -N=read (); M=read (); S=read (); T=read (); t0=read (); Wu      for(intI=1; i<=m;++i) -     { About         intX,y,z;x=read (); Y=read (); z=read (); $ Add (x, y, z); -     } - Tuopu (); -ll ans=0; AAns= (len[t]+ (sum[t]-1) *t0%mod)%MoD; +printf"%lld", (ans+mod)%MoD); the     return 0; -}
View Code

2.DFS procedure

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cmath>5#include <cstring>6#include <map>7#include <queue>8 #defineMoD 100009 #defineINF 336860180Ten #definePI 3.1415926 One #definell Long Long A using namespacestd; - intn,m,s,t,t0,h[100000],tot,ans,sum[100000],len[100000]; - structpo{intV,c,last;} a[500000]; the voidAddintUintVintc) - { -tot++; -A[tot].v=v;a[tot].c=C; +a[tot].last=h[u];h[u]=tot; - } + BOOLv[1000000]; A voidDfsintx) at { -     if(V[x])return ; -v[x]=1; -     if(x==s) -     { -sum[x]=1; len[x]=0; in          return; -     } to     intsumx=0, lenx=0; +      for(intI=h[x];i;i=a[i].last) -     { the      intto=a[i].v; * Dfs (to); $Lenx= (Lenx+a[i].c*sum[to]+len[to])%MoD;Panax NotoginsengSumx= (Sumx+sum[to])%MoD; -     } thesum[x]=sumx;len[x]=Lenx; + } A intMain () the { +scanf"%d%d%d%d%d",&n,&m,&s,&t,&t0); -      for(intI=1; i<=m;i++) $     { $      intX,Y,T;SCANF ("%d%d%d",&x,&y,&t); - Add (y,x,t);  -     } the DFS (t); -Ans= (mod+len[t]+ (sum[t]-1) *t0%mod)%MoD;Wuyicout<<ans; the     return 0; -}
View Code

(?′?‵?) I L???????

JSOI2007 Summer Camp Exam Tour

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.