【POJ 3159】 Candies
【题目链接】
【算法】
差分约束系统
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 30010
#define MAXM 150010
const int INF = 1e8; struct Edge
{
int to,w,nxt;
} e[MAXM];
int i,n,m,a,b,c,tot;
int dis[MAXN],head[MAXN]; inline void add(int u,int v,int w)
{
tot++;
e[tot] = (Edge){v,w,head[u]};
head[u] = tot;
}
inline int spfa()
{
int i,cur,v,w;
stack<int> s;
static bool instack[MAXN];
static int cnt[MAXN];
s.push();
instack[] = true;
dis[] = ;
cnt[] = ;
for (i = ; i <= n; i++) dis[i] = INF;
while (!s.empty())
{
cur = s.top();
s.pop();
instack[cur] = false;
for (i = head[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (dis[cur] + w < dis[v])
{
dis[v] = dis[cur] + w;
if (!instack[v])
{
instack[v] = true;
s.push(v);
cnt[v]++;
if (cnt[v] > n) return -;
}
}
}
}
return dis[n];
} int main()
{ scanf("%d%d",&n,&m);
for (i = ; i <= m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
printf("%d\n",spfa()); return ; }
【POJ 3159】 Candies的更多相关文章
- 【POJ 3159】Candies&&洛谷P3275 [SCOI2011]糖果
来补一下自己很久以前那个很蒟蒻很蒟蒻的自己没有学懂的知识 差分约束,说白了就是利用我们在求最短路的一个\(relax\)操作时的判断的原理 \[dis[v]>dis[u]+disj(u,v)\] ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- 日常开发需要掌握的Maven知识
文章来自:https://www.jianshu.com/p/e224a6dc8f20和https://www.jianshu.com/p/20b39ab6a88c Maven出现之前 jar包默认都 ...
- ThinkPHP---案例2--部门管理功能
[一]部门列表展示 分析: ①控制器DeptController.class.php ②方法showList(不要使用list方法,因为list是关键词) ③模板文件:showList.html 下面 ...
- TWaver HTML5之树形布局
转眼间春节假期已经过完,作为一个职业的程序猿,不知道大家有没有这样的感觉,一天不碰电脑,总觉得生活少点什么.今天是春节后上班的第三天,给大家分享一下我们前段时间的一个需求,需求是这样的:界面中的网元分 ...
- 比较synchronized和读写锁
一.科普定义 这篇博文的两个主角“synchronized”和“读写锁” 1)synchronized 这个同步关键字相信大家都用得比较多,在上一篇“多个线程之间共享数据的方式”中也详细列举他的应用, ...
- background 背景类八大属性
background 背景类八大属性 背景颜色(当同时定义了背景颜色和背景图像时,背景图像覆盖在背景颜色之上) background-image:背景图像 background-repeat:背景图像 ...
- P2041 分裂游戏
P2041 分裂游戏 手推$n=3$是无解的,推断$n>=3$是无解的 证明略,这是道结论题. #include<iostream> #include<cstdio> # ...
- springboot的jsp热部署
使用springboot每次修改jsp都需要重新启动是不是很麻烦呢?以下是解决办法! yml格式 server: servlet: jsp: init-parameters: development: ...
- fiddler培训
fiddler 在客户端和服务器中间做一个代理 ,只能截获http或HTTPS的请求 代理地址127.0.0.1 端口8888 反向代理,正向代理 浏览器上设置代理地址和端口 左边是session ...
- ubuntu root用户登陆
sudo vi /etc/lightdm/lightdm.conf (如果没有该文件则创建,内容如下) [SeatDefaults] user-session=ubuntu greeter-ses ...
- LINUX-APT 软件工具 (Debian, Ubuntu 以及类似系统)
apt-get install package_name 安装/更新一个 deb 包 apt-cdrom install package_name 从光盘安装/更新一个 deb 包 apt-get u ...