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.
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.
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;}
- HDOJ 4396 —— 二维SPFA
- SPFA——HDOJ 1534/3440
- 解题报告-HDOJ-1874(单源最短路径——SPFA)
- dlut1219-spfa+二维dis
- HDOJ 1874 畅通工程续——结构体模拟邻接链表的SPFA算法
- hdu 4396 More lumber is required (二维SPFA)
- BZOJ2763飞行路线|二维SPFA
- USACO传送门|二维SPFA
- 二维spfa [JLOI2011]飞行路线
- bzoj3245 最快路线(二维?spfa)
- HDOJ 3696 Farm Game 【spfa】
- hdoj 2544 最短路 【SPFA】
- HDOJ FATE 二维DP
- POJ1722二维spfa+优先队列优化
- 爱情之路(二维spfa)
- CODEVS 2070 爱情之路(二维SPFA)
- 最短路——SPFA
- 最短路——SPFA
- 白话经典算法系列之四 直接选择排序及交换二个数据的正确实现
- 白话经典算法系列之三 希尔排序的实现
- 白话经典算法系列之二 直接插入排序的三种实现
- 白话经典算法系列之一 冒泡排序的三种实现
- 引用
- HDOJ 4396 —— 二维SPFA
- poj1961 Period (KMP)
- 从一道面试题分析Linux进程+IO缓冲区机制
- 对象和集合初始值设定项
- java 句子中得单词顺序倒过来
- lua结合cocosbuilder开发游戏-不再勾选js controller了
- hdu4758 Walk Through Squares AC自动机(trie图)DP
- CPPUNIT在vs2010实践的编译配置实践
- Esper学习之七:EPL语法(三)