题目传送门

题意:n个人发糖果,B 比 A 多 C的糖果,问最后第n个人比第一个人多多少的糖果

分析:最短路,Dijkstra 优先队列优化可过,SPFA竟然要用栈,队列超时!

代码:

/************************************************
* Author :Running_Time
* Created Time :2015-9-1 19:18:52
* File Name :POJ_3159.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 3e4 + 10;
const int E = 150000 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Edge {
int v, w, nex;
Edge (int _v = 0, int _w = 0) : v (_v), w (_w) {}
bool operator < (const Edge &r) const {
return w > r.w;
}
}edge[E];
int d[N];
int head[N];
int vis[N];
int cnt[N];
int n, m, e; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void Dijkstra(int s) {
memset (vis, false, sizeof (vis));
for (int i=1; i<=n; ++i) d[i] = INF;
d[s] = 0;
priority_queue<Edge> Q; Q.push (Edge (s, 0));
while (!Q.empty ()) {
int u = Q.top ().v; Q.pop ();
if (vis[u]) continue;
vis[u] = true;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v, w = edge[i].w;
if (!vis[v] && d[v] > d[u] + w) {
d[v] = d[u] + w; Q.push (Edge (v, d[v]));
}
}
}
} void SPFA(int s) {
memset (vis, false, sizeof (vis));
memset (d, INF, sizeof (d));
d[s] = 0; vis[s] = true;
stack<int> S; S.push (s);
while (!S.empty ()) {
int u = S.top (); S.pop ();
vis[u] = false;
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v, w = edge[i].w;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = true; S.push (v);
}
}
}
}
} void add_edge(int u, int v, int w) {
edge[e].v = v, edge[e].w = w;
edge[e].nex = head[u]; head[u] = e++;
} int main(void) {
while (scanf ("%d%d", &n, &m) == 2) {
init ();
for (int u, v, w, i=1; i<=m; ++i) {
scanf ("%d%d%d", &u, &v, &w);
add_edge (u, v, w);
}
//Dijkstra (1);
SPFA (1);
printf ("%d\n", d[n]);
} return 0;
}

  

SPFA/Dijkstra POJ 3159 Candies的更多相关文章

  1. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  2. [poj 3159]Candies[差分约束详解][朴素的考虑法]

    题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...

  3. poj 3159 Candies (dij + heap)

    3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...

  4. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  5. POJ 3159 Candies(SPFA+栈)差分约束

    题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] ...

  6. (简单) POJ 3159 Candies,Dijkstra+差分约束。

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

  7. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  8. poj 3159 Candies dijkstra + queue

    题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应 ...

  9. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

随机推荐

  1. scrollTo(String text) and scrollToExact(String text) method of Android Driver not working

    Using the scrollTo(String text) and scrollToExact(String text) method of Android Driver. However the ...

  2. pyspark 连 MongoDB复制集

    解决问题思路: 核心:0-理解pyspark的执行与java jar的关系: 1-看控制台,看日志: 2-jar缺不缺,版本号,放哪里. [root@hadoop1 mylocalRepository ...

  3. opencv IplImage各参数详细介绍以及如何从一个JPEG图像数据指针转换得到IplImage

    这篇文章里介绍得最清楚了.http://blog.chinaunix.net/uid-22682903-id-1771421.html 关于颜色空间  RGB颜色空间已经非常熟悉了.HSV颜色空间需要 ...

  4. react native 之页面跳转

    第一章  跳转的实现 1.component  中添加这行代码 <View style={styles.loginmain}> <Text style={styles.loginte ...

  5. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

  6. asp.net下的cookieName

    https://stackoverflow.com/questions/1017144/rename-asp-net-sessionid Add to your web.config:- <sy ...

  7. Android ViewDragHelper及移动处理总结

    概述 2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用.我们知道在我们实际的开发中往往会涉及到很多的拖动效 ...

  8. 使用PHP对word文档进行操作的方法

    使用php时,因为加密等原因,如果直接用FILE后者OPEN等函数读取WORD的话往往是乱码,原来要使用COM 这是我简单的一个读取并存储到新的WORD上的文件<? // 建立一个指向新COM组 ...

  9. 「BZOJ3438」小M的作物(最小割

    3438: 小M的作物 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1891  Solved: 801[Submit][Status][Discus ...

  10. 区间DP中的环形DP

    vijos1312 链接:www.vijos.org/p/1312 题目分析:经典的环形DP(区间DP) 环形DP,首先解环过程,把数组复制一遍,n个数变成2n个数,从而实现解环 dp[i][j]表示 ...