1835 Academy of Magical Pigs
2010
time limit: 3 sspace limit: 256000 KBtopic rank: Master Master SolvingView Run ResultsTitle Description
Description
Ipig to the legendary School of Witchcraft and Wizardry during the holidays and began training for two months of magical pigs. After a week of theoretical knowledge and a week of basic magic, Ipig learned a lot about the world of swine: it is well known that the world is made up of elements, that elements and elements can be transformed from one another, and that energy is conserved.
Conservation of energy ... Ipig is having a troublesome quiz today. Ipig has learned many kinds of elements in his previous studies, and has learnt the magic that can transform these elements, each of which consumes ipig certain amount of energy. As PKU's top learning pig, let ipig use the least amount of energy to transform from one element to another ... Wait a minute, Ipig's magical guide pig is not so stupid! This time, he brought many samples of the number 1th elements to Ipig, requiring Ipig to use the learned magic to convert them into N elements, in order to increase the difficulty, require each sample conversion process is different. This seemingly difficult task is actually not challenging for Ipig, because he has a solid backing ... Now you!
Note that conversions between two elements can have a variety of magic, and conversions are unidirectional. The process of conversion can be transformed to an element (including the start Element) multiple times, but once the conversion to the target element, a sample of the conversion process is over. The total energy of the ipig is limited, so the maximum number of samples that can be converted must be a finite number. Please refer to the sample for specific examples.
Enter a description
Input Description
The first line of three numbers N, M, E represents the number of elements Ipig know (the number of elements from 1 to N), the number of magic that Ipig has learned, and the total energy of Ipig.
followed by M lines per line three number Si, Ti, ei indicates that Ipig knows a magic that consumes the energy of EI to transform element si into ti.
Output description
Output Description
A number in a row that represents the maximum number of ways that can be completed. Input data guarantees that at least one way can be completed.
Sample input
Sample Input
4 6 14.9
1 2 1.5
2 1 1.5
1 3 3
2 3 1.5
3 4 1.5
1 4 1.5
Sample output
Sample Output
3
Data range and Tips
Data Size & Hint
Sample explanation
There are 4 ways to make a meaningful conversion:
1->4, energy consumption 1.5
1->2->1->4, energy consumption 4.5
1->3->4, energy consumption 4.5
1->2->3->4, energy consumption 4.5
It is obvious that you can only complete 3 of these conversions (choose the first option, the latter three still choose two), that is, a maximum of 3 samples will be converted.
If you change e=14.9 to e=15, you can do all of the above, and the answer becomes 4.
Data size
Data with a total score of not less than 10% satisfies N <= 6,m<=15.
Data with a total score of not less than 20% satisfies N <= 100,m<=300,e<=100 and E and all ei are integers (can be read directly as integer digits).
All data meet 2 <= N <= 5000,1 <= M <= 200000,1<=e<=107,1<=ei<=e,e and all ei is real.
Category labels
Tags Click here to expand2010The
following:
K Short Circuit algorithm
First, SPFA the distance (preprocessing) of finding the end point T to all other points in the reverse graph.
Then from the starting point to use the priority queue for wide search, with CNT records to reach the end of the number of times, when the cnt==k path length is the result.
The direction of the search is determined by a valuation function F=g+dis, where g represents the path length from the starting point to the current point, and the dis represents the shortest path to the current point to the endpoint (that is, the previous preprocessing), each extending the lowest value of the valuation function.
PS: To use the read-in optimization, otherwise it will be T
AC Code:
#include <cstdio>#include<queue>using namespacestd;Const intn=50010;structnode{intV,next; DoubleW;} E1[n<<5],e2[n<<5];intTot,n,m,head1[n],head2[n];Doubledis[n],eps;BOOLVis[n];structdata{DoubleF,g;//f indicates the shortest path through the current node, and G indicates the s-> of the current node int from;//Record current node numberDataintXDoubleYDoubleZ): from(x), F (Y), g (z) {}BOOL operator< (ConstData &a)Const { if(F==A.F)returnG>A.G; returnF>A.F; }};inlinevoidReadint&x) {RegisterCharCh=getchar (); x=0; while(ch>'9'|| ch<'0') ch=GetChar (); while(ch>='0'&&ch<='9') x= (x<<3) + (x<<1) +ch-'0', ch=GetChar ();}voidAddintXintYDoublez) { ++tot; E1[TOT].V=y;e1[tot].w=z;e1[tot].next=head1[x];head1[x]=tot; E2[TOT].V=x;e2[tot].w=z;e2[tot].next=head2[y];head2[y]=tot;}voidSPFA (intS) {//update the shortest distance of->n points per point for(intI=1; i<=n;i++) dis[i]=0x3f3f3f3f; Dis[s]=0; Queue<int>Q; Q.push (S); Vis[s]=1; while(!Q.empty ()) { intH=Q.front (); Q.pop (); VIS[H]=0; for(intI=head2[h];i;i=E2[i].next) { intv=e2[i].v,w=E2[I].W; if(dis[v]>dis[h]+W) {Dis[v]=dis[h]+W; if(!Vis[v]) {Vis[v]=1; Q.push (v); } } } }}intA_star (intSintT) {Priority_queue<data>Q; intCnt=0; DoubleTotal=0; Q.push (Data (s,dis[s),0)); while(!Q.empty ()) {Data H=q.top (); Q.pop (); if(total>eps)returnCnt//return to K-short if(H. from==T) cnt++,total+=H.f; for(intI=head1[h. from];i;i=E1[i].next) {Q.push (data (E1[I].V,H.G+E1[I].W+DIS[E1[I].V],H.G+E1[I].W));//Shortest circuit Update k short Circuit } }}intMain () {intx, y;DoubleZ; Read (n); read (m); scanf ("%LF",&EPS); for(intI=1; i<=m;i++{read (x); Read (y); scanf ("%LF",&z); Add (x, y, z); } SPFA (n); intAns=a_star (1, N); printf ("%d\n", ans-1);//pay attention to the last-1, or it'll be super. return 0;}
1835 Academy of Magical Pigs