|Vijos|图论最短路|P1082 丛林冒险
2016-08-20 18:42
295 查看
http://vijos.org/p/1082
非常有代表性的题目,在SPFA时多加一个判断即可
此题用SPFA有反例,正解搜索,此题解是错误的 (2016.11.27更改)
此问题可解所有体力+权值的最短路问题
非常有代表性的题目,在SPFA时多加一个判断即可
此题用SPFA有反例,正解搜索,此题解是错误的 (2016.11.27更改)
此问题可解所有体力+权值的最短路问题
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> #define ms(i,j) memset(i, j, sizeof(i)); using namespace std; struct node{ int u; int v; int p; int t; node(int u1,int v1, int p1,int t1) : u(u1),v(v1),p(p1),t(t1) {} }; const int maxn = 5000 + 2, maxm = 40000 + 2; vector<node> a[maxm]; int dis[maxn]; int ex[maxn]; int p[maxn]; int n,m,s,t,k; queue<int> q; int main () { scanf("%d%d", &n, &m); for (int i=1;i<=m;i++) { int x,y,c,d; scanf("%d%d%d%d", &x,&y,&c,&d); a[x].push_back(node(x,y,c,d)); a[y].push_back(node(y,x,c,d)); } scanf("%d%d%d", &s, &t, &k); //SPFA p[s] = k; ms(ex,false); ex[s]=true; ms(dis,27); dis[s]=0; dis[t]=100000000; q.push(s); while(!q.empty()) { int u = q.front();q.pop();ex[u]=false; for (int i=0;i<a[u].size();i++) { node pn = a[u][i]; if (dis[pn.v]>dis[u]+pn.t&&p[u]>=pn.p) { dis[pn.v]=dis[u]+pn.t; p[pn.v] = p[u]-pn.p; if (!ex[pn.v]) { ex[pn.v] = true; q.push(pn.v); } } } } printf("%d\n", (dis[t]==100000000) ? -1 : dis[t]); return 0; }
相关文章推荐
- |Vijos|图论最短路|P1446 最短路上的统计
- |Vijos|图论最短路|P1155 集合位置
- |Vijos|图论最短路|P1391 想越狱的小杉
- |Vijos|图论最短路|P1046 观光旅游
- 图论算法(5) --- 双向广搜求最短路(Bidirectional Breadth First Search)
- PKU最短路及其衍生出的图论题
- 图论算法--单源最短路--dijkstra算法(优先队列优化)
- UVa567_Risk(最短路)(小白书图论专题)
- Hdu图论最短路基础题
- |Tyvj|图论最短路|P1083 分糖果
- ⑨讲图论第四课: Dijkstra算法求最短路
- 图论之最短路入门
- 模板整理——图论·最短路·dijkstra+堆
- codevs 1021 玛丽卡 图论:最短路
- 图论浅析--最短路之SPFA
- 【vijos】【建反图最短路】最优贸易
- 洛谷P1346-电车【日常图论,最短路,SPFA】
- 图论——最短路
- POJ - 3268 Silver Cow Party(图论/dijkstra最短路)
- hdu 2544 最短路(图论:迪杰斯特拉||弗洛伊德算法)