POJ 3159 Candies(SPFA+栈)差分约束
题目链接: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+栈)差分约束的更多相关文章
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- Candies POJ - 3159 (最短路+差分约束)
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...
- POJ 3159 【朴素的差分约束】
好吧终于知道什么是“高大上”的差分约束了.嗷嗷 题意: 小朋友们分糖果,某个小朋友不想另外一个小朋友分到的糖果数比自己多N块以上. 求编号为N的小朋友最多比编号为1的小朋友多分多少块糖果. 思路: 差 ...
- POJ 3159 Candies (栈优化spfa)
Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...
随机推荐
- struts2类型转换与校验总结
1.struts2的类型转换分为全部变量转变和局部变量转变. 2.struts2对8中常见的基本类型的属性变量,可以自动转换.如果是User对象,可以手动简历UserAction-coversion. ...
- mysql问题Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)的解决方法
在mysql命令行界面输入 mysql> set old_passwords=0;mysql> SET PASSWORD FOR hhds_test=PASSWORD('hhds_test ...
- 干掉cmd:windows下使用linux命令行
对于喜欢用命令行的朋友们,在windows下面使用cmd窗口是不是很不爽?复制不方便?不能随意放大缩小?如果需要多个控制台要多个窗口?....各种不爽 一.基础工具 如果你也不爽,那就对了,所以给大家 ...
- InfiniBand
Mellanox InfiniBand卡线缆性能延迟性测试程序源码,C源码实现操作mysql库,实现简单的增删改查,代码当前用的是增插入20000条数据 具体见源码 #include <mysq ...
- CST和GMT时间的区别
CST和GMT时间的区别 今天遇到一个奇怪的问题,在服务器端通过 c# 获取当前时间为 Fri Aug 28 09:37:46 CST 2009, 转化为 GMT时间为:28 Aug 2009 01: ...
- 【多线程】Java并发编程:Lock(转载)
原文链接:http://www.cnblogs.com/dolphin0520/p/3923167.html Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized ...
- UVALive 5886 The Grille (模拟)
The Grille 题目链接: http://acm.hust.edu.cn/vjudge/problem/26634 Description http://7xjob4.com1.z0.glb.c ...
- svn switch relocate用法
svn info svn info 得到 Path: . Working Copy Root Path: /Users/chunhuizhao/phpworkspace/buptef_wxpay/tr ...
- 安装php5.5
安装php5.5 ./configure --prefix=/usr/local/php5.5.14/ --with-apxs2=/usr/local/apache2.2.27/bin/apxs -- ...
- codeforces 601A The Two Routes(最短路 flody)
A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...