HDOJ 4396 —— 二维SPFA

More lumber is required

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 964    Accepted Submission(s): 398


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.
 

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
 
题意是在不小于n步的路径中找到一条无向图的最短路。
PS:邻接表和邻接矩阵数组要开到点的平方。。。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 1000000 + 500;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 1 << 29;
const int IMIN = 0x80000000;
const double e = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
int n , m ;
int em , guard;
queue < pair<int , int> > q;
int to[MAXN] , cap[MAXN] , dp[5500][110] , vis[5500][110] , next[MAXN] , edge[MAXN];
int change(int x)
{
    return x <= guard ? x : guard;
}
void addedge(int u , int v , int w)
{
    to[em] = v;
    cap[em] = w;
    next[em] = edge[u];
    edge[u] = em++;
}
void spfa(int s)
{
    FORR(i , 0 , n)FORR(j , 0 , 52)
    {
        dp[i][j] = INF;
        vis[i][j] = false;
    }
    dp[s][0] = 0;
    vis[s][0] = true;
    q.push(mk(s , 0));
//    outstars
    while(!q.empty())
    {
        int x = q.front().first , y = q.front().second ; q.pop();
        int p = edge[x];
//        outstars
        while(p != -1)
        {
//            outstars
            if(dp[x][y] + cap[p] < dp[to[p]][change(y + 1)])
            {
                dp[to[p]][change(y + 1)] = dp[x][y] + cap[p];
                if(!vis[to[p]][change(y + 1)])
                {
                    vis[to[p]][change(y + 1)] = true;
                    q.push(mk(to[p] , change(y + 1)));
                }
            }
            p = next[p];
        }
        vis[x][y] = false;
    }

}
int main()
{
    while(~scanf("%d%d" , &n , &m))
    {
        em = 0;
        clr(next , -1) , clr(edge , -1);
        while(m--)
        {
            int u ,  v , w;
            scanf("%d%d%d" , &u , &v , &w);
//            outstars
            addedge(u , v ,w);
//            outstars
            addedge(v , u , w);
        }
        int s , t;
        scanf("%d%d%d" , &s , &t , &guard);
        guard = guard % 10 == 0 ? guard / 10 : guard / 10 + 1;
//        cout << guard << endl;
//        outstars
        spfa(s);
//        outstars
        if(dp[t][guard] < INF)printf("%d\n", dp[t][guard]);
        else printf("-1\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值