More lumber is required
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 909 Accepted Submission(s): 377
Problem 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.
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.
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
Author
Wanghang----School of Software Technology, Dalian University of Technology
Source
Recommend
zhuyuanchen520
思路:dis[u][k] 终点为S->u 经过k条边的最优结果,然后就是水了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=5009;
const int oo=1e9;
int dis[mm][550],head[mm],edge;
bool vis[mm][550];
class Edge
{
public:int v,next,dis;
}e[mm*mm];
int n,m,S,T,K;
void data()
{
clr(head,-1);edge=0;
}
void add(int u,int v,int c)
{
e[edge].v=v;e[edge].dis=c;e[edge].next=head[u];head[u]=edge++;
}
int to(int x)
{
if(x<K)return x;
return K;
}
void spfa()
{
FOR(i,0,n)FOR(j,0,K)dis[i][j]=oo,vis[i][j]=0;
dis[S][0]=0;
queue<pair<int,int> >Q;
Q.push(make_pair(S,0));
pair<int,int >z;int u,v,dep;
while(!Q.empty())
{
z=Q.front();Q.pop();
u=z.first;dep=z.second;
vis[u][dep]=0;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(dis[v][to(dep+1)]>dis[u][dep]+e[i].dis)
{
dis[v][to(dep+1)]=dis[u][dep]+e[i].dis;
if(vis[v][to(dep+1)])continue;
vis[v][to(dep+1)]=1;Q.push(make_pair(v,to(dep+1)));
}
}
}
}
int main()
{
int a,b,c;
while(~scanf("%d%d",&n,&m))
{ data();
FOR(i,1,m)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
scanf("%d%d%D",&a,&b,&c);
S=a;T=b;K=c;
if(K%10!=0)K=K/10+1;
else K=K/10;
spfa();
if(dis[T][K]!=oo)printf("%d\n",dis[T][K]);
else printf("-1\n");
}
return 0;
}
本文介绍了一种针对游戏资源收集的最短路径算法。在特定的游戏中,玩家需通过规划最优路径收集木材来建造中央城镇。算法考虑了多个锯木厂间的连接道路及其消耗的时间成本,旨在找到达到指定目标所需的最少时间。
284

被折叠的 条评论
为什么被折叠?



