cdoj915-II (shortest short of length not less than k) "SPFA"

Source: Internet
Author: User

http://acm.uestc.edu.cn/#/problem/show/915

Fang Teacher's fen IITime limit:10000/5000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus

The teacher calculated the time taken by the person who walked the longest time. So there is a special fen Shen (said to be the teacher of Fang!) Don't want to be so prim to go the shortest way! Due to the particularity of this fen, this one can be used as a two-way edge for unidirectional edges. But this special one wants to go the shortest way at the same time, requiring at least a k -Bar.

Input

There are multiple sets of data

First row two integersN,m(1≤n≤ , 1≤m≤100000) indicates that there are n classrooms,m -bar edges.

NextmRow, per line3Numberu,v, T. Represents u, andv has an edge of length t .

Last line of three integerss,T, K, indicating the starting point, end point, at least k(k≤) edge.

Output

An integer that represents the shortest path length. If no solution output −1.

One row for each group of data.

Sample Input and output
Sample Input Sample Output
4 41 2 12 3 21 3 1003 4 11 3 5
7

The most basic: the shortest circuit on the basis of the restriction conditions: not less than the length of K. Therefore, the distance array dis can be added a one-dimensional state, dis[i][j] means that the arrival node I has passed the J-edge of the shortest path, to achieve the purpose. The use of Spfa,dijkstra is also available here.

Here's a simple SPFA algorithm:

• Get a queue. Start by pressing the starting point in. Each pop-up point, and the point has the edge directly connected to the point is updated again, if the current calculated distance is less than the distance calculated before, the value is changed to the current calculation, and then see the new point, if not in the queue to press into the queue. • When the queue is empty, you can get the distance from all points to the starting point. Code:
1#include <fstream>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <queue>6 7 using namespacestd;8 9 Const intinf=0x7fffffff;Ten Const intn=5005; One Const intm=200005; A Const intk=Wuyi; -queue<pair<int,int> >Q; - intN,m,s,t,k;//s->begin; t->end. the intHead[n],later[m],u[m],v[m],w[m]; - intDIS[N][K];//reached the node I has passed K-bar, at this time the shortest distance - BOOLB[n][k]; -  + voidSPFA (); -  + intMain () A { at     //freopen ("d:\\input.in", "R", stdin); -     //freopen ("D:\\output.out", "w", stdout); -      while(~SCANF ("%d%d",&n,&m)) { -memset (head,-1,sizeof(head)); -          for(intI=0; i<m;i++){ -scanf"%d%d%d",&u[i],&v[i],&w[i]); inlater[i]=Head[u[i]]; -head[u[i]]=i; tou[i+m]=V[i]; +v[i+m]=U[i]; -w[i+m]=W[i]; thelater[i+m]=head[u[i+m]]; *head[u[i+m]]=i+m; $         }Panax Notoginsengscanf"%d%d%d",&s,&t,&k); - SPFA (); the         if(Dis[t][k]==inf) puts ("-1"); +         Elseprintf"%d\n", Dis[t][k]); A     } the     return 0; + } - voidSPFA () { $      for(intI=1; i<=n;i++) $          for(intj=0; j<=k;j++) -dis[i][j]=INF; -dis[s][0]=0; theQ.push (Make_pair (s),0)); -      while(!Q.empty ()) {Wuyipair<int,int> tmp=Q.front (); the Q.pop (); -b[tmp.first][tmp.second]=0; Wu          for(inti=head[tmp.first];i!=-1; i=Later[i]) { -             intTt=min (tmp.second+1, k);//the number of sides greater than K is treated as K . About             if(dis[v[i]][tt]>dis[tmp.first][tmp.second]+W[i]) { $dis[v[i]][tt]=dis[tmp.first][tmp.second]+W[i]; -                 if(!B[v[i]][tt])//Avoid repeating the queue -Q.push (Make_pair (V[I],TT)), b[v[i]][tt]=1; -             } A         } +     } the}

cdoj915-II (shortest short of length not less than k) "SPFA"

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.