题目链接:http://poj.org/problem?id=3159

题意:给出m给 x 与y的关系。当中y的糖数不能比x的多c个。即y-x <= c  最后求fly[n]最多能比so[1]
多多少糖?

差分约束问题, 就是求1-n的最短路,  队列实现spfa
会超时了,改为栈实现,就可以

有负环时,用栈比队列快

数组开小了,不报RE,报超时 ,我晕

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
const int N = 210;
const int maxn = 30100;
const int maxm = 200000;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define init(a) memset(a,0,sizeof(a))
#define MIN INT_MIN
#define MAX INT_MAX
#define LL long long
using namespace std;
int max(int a,int b){if(a>b)return a; else return b;}
int min(int a,int b){if(a<b)return a; else return b;}
const int INF=0x3f3f3f3f;
struct node
{
int v,w;
int next;
}edge[maxm];
int head[maxn];
bool vis[maxn];
int dis[maxn];
int cnt;
void add(int a,int b,int w)//加边
{
edge[cnt].v=b;
edge[cnt].w=w;
edge[cnt].next=head[a];
head[a]=cnt++;
}
void SPFA(int s,int n)
{
//stack<int>stk;
int stk[100000];
int top = 0; //stk.push(s);
stk[top++] = s;
FOR(i,1,n+1)
{dis[i] = INF;
vis[i] = 0;
}
dis[s] = 0;
vis[s] = 1;
while(top!=0)
{
int u=stk[--top];
//stk.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!vis[v])
{
vis[v]=true;
stk[top++] = v;
}
}
} }
}
void initt()
{
cnt=0;
memset(head,-1,sizeof(head));
}
int main()
{
int n,m;
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
initt();
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
SPFA(1,n);
printf("%d\n",dis[n]);
}
return 0;
}

POJ 3159 Candies(SPFA+栈)差分约束的更多相关文章

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

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

  2. Candies POJ - 3159 (最短路+差分约束)

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  3. POJ 3159 【朴素的差分约束】

    好吧终于知道什么是“高大上”的差分约束了.嗷嗷 题意: 小朋友们分糖果,某个小朋友不想另外一个小朋友分到的糖果数比自己多N块以上. 求编号为N的小朋友最多比编号为1的小朋友多分多少块糖果. 思路: 差 ...

  4. POJ 3159 Candies (栈优化spfa)

    Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...

  5. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

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

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

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

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

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

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

  9. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

随机推荐

  1. 【LR】OSGI性能测试实例

    其实我们就两点Ø  确定测试登录最大并发用户数:Ø  事务平均响应时间 (两个查询) 得到这个任务 如何展开测试工作呢? 一.WindowsResources 设置(其实不监控 设不设都行 我感觉)  ...

  2. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_all_from_list(self, locator)

    def select_all_from_list(self, locator): """Selects all values from multi-select list ...

  3. cocos2dx 内存管理的理解

    关于引擎内存管理的细节,网上有大量的详解,这里概括一下: cocos2d-x 的世界是基于 CCObject 类构建的,所以内存管理的本质就是管理一个个 CCObject. //CCObject 内部 ...

  4. vim开发环境配置

    一.大饱眼福 看了效果图,肯定有人说, 这都有啥功能?就花哨? 告诉你,你说花哨就错了,开玩笑?我们程序猿可都是实打实的人,说谎都不会,咋会忽悠人呢. 下面我来告诉你,这都有些什么功能: 文件索引功能 ...

  5. scanf()/getchar()和gets()深入分析

    C/C++学习笔记1 - 深入了解scanf()/getchar()和gets()等函数 ---------------------------------------------------- | ...

  6. MVC中使用AuthorizeAttribute做身份验证操作

    代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...

  7. 借助LVS+Keepalived实现负载均衡(转)

    原文:http://www.cnblogs.com/edisonchou/p/4281978.html 一.负载均衡:必不可少的基础手段 1.1 找更多的牛来拉车吧 当前大多数的互联网系统都使用了服务 ...

  8. Java Applet and ServiceLoader

    http://stackoverflow.com/questions/14062813/java-applet-and-serviceloader

  9. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

  10. 粗解Xcode 5新特性: Asset Catalogs

    原文:http://schlu.org/2013/10/01/Xcode-Asset-Catalogs.html 基础知识 在今年的WWDC大会上苹果介绍了Asset Catalogs.Asset C ...