HDOJ 4396 —— 二维SPFA

来源:互联网 发布:最新金山画王软件 编辑:程序博客网 时间:2024/05/21 08:35

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 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.
 

Output
For each test case, print the result in a single line.
 

Sample Input
4 41 2 12 3 21 3 1003 4 11 3 50
 

Sample Output
7
 

Author
Wanghang----School of Software Technology, Dalian University of Technology
 

Source
2012 Multi-University Training Contest 10
 

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 1000000007typedef 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;}


原创粉丝点击