您的位置:首页 > 产品设计 > UI/UE

hdu 4396 More lumber is required(最短路,SPFA,4级)

2013-08-04 20:20 513 查看

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



[align=left]Problem Description[/align]
“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.

[align=left]Input[/align]
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 Ath sawmill and Bth 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.

[align=left]Output[/align]
For each test case, print the result in a single line.

[align=left]Sample Input[/align]

4 4
1 2 1
2 3 2
1 3 100
3 4 1
1 3 50


[align=left]Sample Output[/align]

7


[align=left]Author[/align]
Wanghang----School of Software Technology, Dalian University of Technology

[align=left]Source[/align]
2012 Multi-University Training Contest 10

[align=left]Recommend[/align]
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: