标签:
Description
“More lumber(木材) is required” When the famous warcrafts player Sky wants to build a Central Town, he finds there is not enough lumber to build his Central Town. So he needs to collect enough lumber. He lets farmer John to do this work.
There are several Sawmills(锯木厂) have already been built in the world, around them are large forests. Sawmills are connected by bidirectional(双向的) roads (a sawmill can be connected to itself). When he passes a road, he will get 10 lumber and consume(消耗) a certain time. Sky needs K lumber. So John needs collect as least K lumber.
Sawmills are 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 his route(按某路线发送) carefully because Sky wants to build this Central Town as early as possible. He turns you for help. Please 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.
Input
There are multiply test cases, in each test case:
The first line is two integers(整数) N (1<=N<=5000), M (0<=M<=100000) represent the number of sawmills and the number 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 A th sawmill and B th sawmill, and pass this road will cost C time.(The sawmills are labeled from 1 to N).
The last line is three integers S T K (1<=S, T<=N; 0<=K<=500), as mentioned as description.
Output
For each test case, print the result in a single line.
Sample Input
4 4
1 2 1
2 3 2
1 3 100
3 4 1
1 3 50
Sample Output
7
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 5005;
const int M = 200005;
const int NN = 505;
const int Lum = 10;
const int INF = 0x3f3f3f3f;
int vis[N][NN], d[N][NN], en;
int head[M];
int n, m, s, t, k;
struct node {
int to, dis, next;
}edge[M];
void addEdge(int u,int v,int x) {
edge[en].to = v;
edge[en].next = head[u];
edge[en].dis = x;
head[u] = en++;
edge[en].to = u;
edge[en].next = head[v];
edge[en].dis = x;
head[v] = en++;
}
struct Node{
int u, m;
};
void SPFA() {
queue<Node> Q;
for(int i = 1; i <= n; i++) {
for (int j = 0; j <= NN; j++) {
d[i][j] = INF;
vis[i][j] = 0;
}
}
d[s][0] = 0;
vis[s][0] = 1;
Q.push((Node){s, 0});
while(!Q.empty()) {
int u = Q.front().u;
int m = Q.front().m;
vis[u][m] = 0;
Q.pop();
int temp = m + Lum;
if (temp >= NN) continue;
if (temp > k) temp = k;
for(int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if(d[u][m] + edge[i].dis < d[v][temp]) {
d[v][temp] = d[u][m] + edge[i].dis;
if(!vis[v][temp]) {
Q.push((Node){v, temp});
vis[v][temp] = 1;
}
}
}
}
}
void input() {
int u, v, c;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &u, &v, &c);
addEdge(u, v, c);
}
scanf("%d %d %d", &s, &t, &k);
}
int main() {
while (scanf("%d %d", &n, &m) == 2) {
en = 0;
memset(head, -1, sizeof(head));
input();
if (k % 10) {
k = 10 * ((k / 10) + 1);
}
SPFA();
if (d[t][k] == INF) {
printf("-1\n");
} else printf("%d\n", d[t][k]);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu 4396 More lumber is required(最短路)
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47171797