HDU More lumber is required

More lumber is required

Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 280    Accepted Submission(s): 78
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.
 
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.
 
SampleInput
4 4
1 2 1
2 3 2
1 3 100
3 4 1
1 3 50
 
SampleOutput
7
 
Author
Wanghang----School of Software Technology, Dalian University of Technology
 
 
 

题目大意:

n个点,m条双向边。给你起点s,终点t,以及k。问你从s到t至少得到k分的最短路。边有自环,且边可以重复走。边每走一次获得10分。

(1<=N<=5000)(0<=M<=100000) (1<=S, T<=N; 0<=K<=500)

题解:

二维spfa,d[x][j]表示到x这个点经过j条边的最短路,有个重要的优化是:当L>K时把它并入到走K条边的状态中。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;

const int INF=0x3f3f3f3f;
const int N=5010;

int n,m,cnt,ans;
int s,t,k;
int dp[N][510],vis[N][510],head[N];

struct Edge{
    int u,v,w;
    int next;
}edge[200010];

void Init(){
    cnt=0;
    memset(head,-1,sizeof(head));
}

void addedge(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++;
}

struct node{
    int a,b;
    node(){}
    node(int x,int y):a(x),b(y){}
};

void SPFA(){
    queue<node> q;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    int i,j;
    for(i=0;i<=n;i++)
        for(j=0;j<=k;j++)
            dp[i][j]=INF;
    dp[s][0]=0;
    vis[s][0]=1;
    q.push(node(s,0));
    while(!q.empty()){
        node u=q.front();
        q.pop();
        vis[u.a][u.b]=0;    //因为可以走重边??
        if(u.a==t && u.b==k)
            ans=min(ans,dp[t][k]);
        for(i=head[u.a];i!=-1;i=edge[i].next){
            node cur;
            cur.a=edge[i].v, cur.b=u.b+10;
            if(cur.b>=k)
                cur.b=k;
            if(dp[cur.a][cur.b]>dp[u.a][u.b]+edge[i].w){
                dp[cur.a][cur.b]=dp[u.a][u.b]+edge[i].w;
                if(!vis[cur.a][cur.b]){
                    vis[cur.a][cur.b]=1;
                    q.push(cur);
                }
            }
        }
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d",&n,&m)){
        Init();
        int u,v,w;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        scanf("%d%d%d",&s,&t,&k);
        ans=INF;
        SPFA();
        printf("%d\n",ans==INF?-1:ans);
    }
    return 0;
}

 

 

 

 

 

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<algorithm>
 6 
 7 using namespace std;
 8 
 9 const int INF=0x3f3f3f3f;
10 
11 int n,m,s,t,k,cnt;
12 int head[5010],next[200010],to[200010];
13 int dis[5010][60],w[200010];
14 
15 bool vis[5010][60];
16 
17 void addedge(int u,int v,int cost){
18     to[cnt]=v;
19     w[cnt]=cost;
20     next[cnt]=head[u];
21     head[u]=cnt++;
22 }
23 
24 void SPFA(int src){
25     queue<pair<int,int> > q;
26     while(!q.empty())
27         q.pop();
28     int i,j;
29     for(i=1;i<=n;i++)
30         for(j=1;j<=k;j++)
31             dis[i][j]=INF;
32     q.push(make_pair(src,0));
33     while(!q.empty()){
34         int u=q.front().first;
35         int num=q.front().second;
36         q.pop();
37         vis[u][num]=0;      //因为可以走重边
38         for(i=head[u];i!=-1;i=next[i]){
39             int tmp=num+(num<k?1:0);
40             if(dis[u][num]+w[i]<dis[to[i]][tmp]){
41                 dis[to[i]][tmp]=dis[u][num]+w[i];
42                 if(!vis[to[i]][tmp]){
43                     q.push(make_pair(to[i],tmp));
44                     vis[to[i]][tmp]=1;
45                 }
46             }
47         }
48     }
49 }
50 
51 int main(){
52 
53     //freopen("input.txt","r",stdin);
54 
55     while(~scanf("%d%d",&n,&m)){
56         cnt=0;
57         memset(head,-1,sizeof(head));
58         int u,v,cost;
59         for(int i=0;i<m;i++){
60             scanf("%d%d%d",&u,&v,&cost);
61             addedge(u,v,cost);
62             addedge(v,u,cost);
63         }
64         scanf("%d%d%d",&s,&t,&k);
65         k=(k-1)/10+1;
66         SPFA(s);
67         printf("%d\n",dis[t][k]<INF?dis[t][k]:-1);
68     }
69     return 0;
70 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值