More lumber is requiredTime limit:10000/5000 MS (java/others) Memory limit:102400/102400 K (java/others)
Total submission (s): 1392 Accepted Submission (s): 538
Problem Description "More lumber was required" when the famous warcrafts player Sky wants to build a central town, he finds There is isn't enough lumber to build he central town. So he needs to collect enough lumber. He lets farmer John to do the work.
There is several sawmills has already been built in the world, around them is large forests. Sawmills is connected by bidirectional roads (a sawmill can is connected to itself). When he passes a road, he'll get lumber and consume a certain time. Sky needs K lumber. So John needs collect as least K lumber.
Sawmills is labeled from 1 to N. John initiates at Sawmill S. When he finishes his work, Sky gives him another work:arrive at Sawmill T, and build the central town. John needs to design he route carefully because Sky wants to build this central town as early as possible. He turns you to help. Him calculate the minimum time he needs to finish this work (collect enough lumber and build the central town) . If Impossible just print-1.
You can read the Sample Input and Output for more information.
Inputthere is multiply test cases, in each test case:
The first line is the integers N (1<=n<=5000), M (0<=m<=100000) represent the number of sawmills and the Numbe R of the roads.
The next M line is three integers a B C (1<=a, b<=n; 1<=c<=100), means there exists A road connected Ath sawmi ll and Bth sawmill, and pass this road would cost C time. (The sawmills is labeled from 1 to N).
The last line is three integers S T K (1<=s, t<=n; 0<=k<=500), as mentioned.
Outputfor each test case, print the result in a single line.
Sample Input
4 41 2 12 3 21 3 1003 4 11 3 50
Sample Output
7
Authorwanghang----School of software technology, Dalian University of technology
Source2012 multi-university Training Contest 10
Recommendzhuyuanchen520 | We have carefully selected several similar problems for you:4390 4398 4397 4395 4394 AC Code
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <string > #include <iostream> #define MIN (A, b) (a>b?b:a) #define INF 0xfffffffusing namespace Std;int dis[5050][550], Vis[5050][550],n,m,cnt,head[5050];int s,t,k,ans;struct s{int U,v,w,next;} Edge[100010*2];struct node{int x, y;} a,temp;void Add (int u,int v,int W) {edge[cnt].u=u;edge[cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;} void Spfa () {int i,j;for (i=0;i<=n;i++) {for (j=0;j<=k;j++) {Dis[i][j]=inf;}} memset (vis,0,sizeof (VIS));d Is[s][0]=0;vis[s][0]=1;queue<node>q;a.x=s;a.y=0;q.push (a); while (!q.empty ()) {A =q.front (); Q.pop (); vis[a.x][a.y]=0;if (a.x==t&&a.y==k) {ans=min (ans,dis[a.x][a.y]);} for (I=head[a.x];i!=-1;i=edge[i].next) {temp.x=edge[i].v;temp.y=a.y+1;if (temp.y>=k) temp.y=k;if (dis[temp.x][ TEMP.Y]>DIS[A.X][A.Y]+EDGE[I].W) {dis[temp.x][temp.y]=dis[a.x][a.y]+edge[i].w;if (!vis[temp.x][temp.y]) {vis[ Temp.x][temp.y]=1;q.push (temp);}}}} int main () {//int n, M;while (scanf ("%d%d", &n,&m)!=eof) {Cnt=0;memset (head,-1,sizeof (head)); int I;for (i=0;i<m;i++) {int u,v, W;SCANF ("%d%d%d", &u,&v,&w); add (u,v,w); add (v,u,w);} scanf ("%d%d%d", &s,&t,&k), if (k%10) K=K/10+1;ELSEK=K/10;ANS=INF;SPFA (), if (ans==inf) printf (" -1\n"); elseprintf ("%d\n", ans);}}
Hdoj Topic 4396 More lumber is required (least shorted by K-bar)