Academy of Magical Pigs (Codevs 1835)

Source: Internet
Author: User

Title 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.

/*The K short-circuit algorithm first SPFA find out the inverse graph to find the end point T to all other points of the distance (preprocessing) 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 obtained. 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. */#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#defineN 5010#defineM 200010using namespacestd;inthead1[n],head2[n],vis[n],cnt,n,m;DoubleZo,dis[n],tot;structnode{intV,pre; Doublet;}; Node E1[m],e2[m];structnode{int  from;Doublef,g; BOOL operator< (Node c)Const    {        if(c.f!=f)returnc.f<F; returnc.g<G; }}; Node x;voidAddintIintXintYDoublez) {E1[I].V=y; E1[I].T=Z; E1[i].pre=Head1[x]; HEAD1[X]=i; E2[I].V=x; E2[I].T=Z; E2[i].pre=Head2[y]; Head2[y]=i;}voidSPFA (intSintt) {     for(intI=1; i<=n;i++) dis[i]=m*N; Queue<int>Q; Vis[s]=1;d is[s]=0;    Q.push (s);  while(!Q.empty ()) {        intx=Q.front ();        Q.pop (); VIS[X]=0;  for(intI=head2[x];i;i=e2[i].pre)if(dis[e2[i].v]>dis[x]+e2[i].t) {DIS[E2[I].V]=dis[x]+e2[i].t; if(!VIS[E2[I].V]) {VIS[E2[I].V]=1;            Q.push (E2[I].V); }          }    }} voidA_star (intSintt) {Priority_queue<Node>Q; X. from=s;x.g=0; x.f=Dis[s];    Q.push (x);  while(!Q.empty ()) {x=q.top (); Q.pop (); if(Tot>zo)return; if(x. from==t) {++CNT; Tot+=x.f; }         for(intI=head1[x. from];i;i=e1[i].pre)            {Node to; To. from=e1[i].v; TO.G=x.g+e1[i].t; TO.F=to.g+DIS[E1[I].V];        Q.push (to); }    }}intMain () {scanf ("%D%D%LF",&n,&m,&zo);  for(intI=1; i<=m;i++)    {        intx, y;DoubleZ; scanf ("%D%D%LF",&x,&y,&z);    Add (i,x,y,z); } SPFA (N,1); A_star (1, N); printf ("%d", cnt-1); return 0;}
View Code

Academy of Magical Pigs (Codevs 1835)

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.