hdu 4396 More lumber is required
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
题目大意:有n家伐木场,伐木场之间有m条路径,路径包括路径的起始点和目标点和耗时。然后告诉你起点s和终点t,还有需要的木材数量k。每经过一家伐木场,木材数量加10。现在问,从起点出发,到达终点并收集满k的木材,所需要的最短时间。
解题思路:SPFA。开一个二维d[i][j]数组,记录从起点出发,到达i点,并且收集j的木材所耗费的最小时间,用队列不断维护这个数组就可以了。k有可能不是10的倍数,这个时候要对k进行处理。
#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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。