POJ-3159.Candies.(差分约束 + Spfa)
Candies
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 40407 | Accepted: 11367 |
Description
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
Input
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
Output
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
- 2 2
- 1 2 5
- 2 1 4
Sample Output
- 5
Hint
Source
- #include <cstdio>
- #include <cstring>
- #include <stack>
- using namespace std;
- const int maxn = , maxe = , INF = 0x3f3f3f3f;
- struct node {
- int to, w, next;
- }edge[maxe];
- int num, head[maxn], dist[maxn];
- bool vis[maxn];
- void addedge(int u, int v, int cost) {
- edge[num].to = v;
- edge[num].w = cost;
- edge[num].next = head[u];
- head[u] = num ++;
- }
- void spfa(int start, int n) {
- stack <int> Q;
- for(int v = ; v <= n; v ++) {
- if(v == start) {
- Q.push(v);
- vis[v] = true;
- dist[v] = ;
- }
- else {
- vis[v] = false;
- dist[v] = INF;
- }
- }
- while(!Q.empty()) {
- int u = Q.top(); Q.pop();
- vis[u] = false;
- for(int i = head[u]; i != -; i = edge[i].next) {
- int v = edge[i].to;
- if(dist[v] > dist[u] + edge[i].w) {
- dist[v] = dist[u] + edge[i].w;
- if(!vis[v]) {
- vis[v] = true;
- Q.push(v);
- }
- }
- }
- }
- }
- int main () {
- int n, m, a, b, cost;
- while(~scanf("%d %d", &n, &m)) {
- num = ;
- memset(head, -, sizeof head);
- for(int i = ; i <= m; i ++) {
- scanf("%d %d %d", &a, &b, &cost);
- addedge(a, b, cost);
- }
- spfa(, n);
- printf("%d\n", dist[n]);
- }
- return ;
- }
或者用数组模拟栈也可以:
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int maxn = , maxe = , INF = 0x3f3f3f3f;
- struct node {
- int to, w, next;
- }edge[maxe];
- int num, head[maxn], dist[maxn];
- int Q[maxn];
- bool vis[maxn];
- void addedge(int u, int v, int cost) {
- edge[num].to = v;
- edge[num].w = cost;
- edge[num].next = head[u];
- head[u] = num ++;
- }
- void spfa(int start, int n) {
- int top = ;
- for(int v = ; v <= n; v ++) {
- if(v == start) {
- Q[top ++] = v;
- vis[v] = true;
- dist[v] = ;
- }
- else {
- vis[v] = false;
- dist[v] = INF;
- }
- }
- while(top != ) {
- int u = Q[-- top];
- vis[u] = false;
- for(int i = head[u]; i != -; i = edge[i].next) {
- int v = edge[i].to;
- if(dist[v] > dist[u] + edge[i].w) {
- dist[v] = dist[u] + edge[i].w;
- if(!vis[v]) {
- vis[v] = true;
- Q[top ++] = v;
- }
- }
- }
- }
- }
- int main () {
- int n, m, a, b, cost;
- while(~scanf("%d %d", &n, &m)) {
- num = ;
- memset(head, -, sizeof head);
- for(int i = ; i <= m; i ++) {
- scanf("%d %d %d", &a, &b, &cost);
- addedge(a, b, cost);
- }
- spfa(, n);
- printf("%d\n", dist[n]);
- }
- return ;
- }
POJ-3159.Candies.(差分约束 + Spfa)的更多相关文章
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- POJ 3159 Candies 差分约束dij
分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ——3159Candies(差分约束SPFA+前向星+各种优化)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 28071 Accepted: 7751 Descrip ...
- POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS Memory Limit: 131072K Total Submiss ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- 【poj3169】【差分约束+spfa】
题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...
随机推荐
- IDEA汉化
1.- 汉化包 提取码: 4mbq 2.打开IDEA,执行下列操作:在主界面选择File → Settings → Appearance&Behavior → Appearance → 勾选O ...
- 怎么安装Scrapy框架以及安装时出现的一系列错误(win7 64位 python3 pycharm)
因为要学习爬虫,就打算安装Scrapy框架,以下是我安装该模块的步骤,适合于刚入门的小白: 一.打开pycharm,依次点击File---->setting---->Project---- ...
- [UE4]AttachToComponent的AttachmentRule
官方文档 KeepRelative 将当前相对转换保持为新父级的相对转换 KeepWorld 自动计算相对变换,使附着的组件保持相同的世界变换 SnapToTarget 捕捉转换到附着点
- 使用Linux的环境变量
许多程序和脚本都使用环境变量来获取系统信息,并存储临时数据和配置信息: 1.什么是环境变量 用来存储关于shell会话和工作环境的信息,就叫做环境变量: bash shell下两种类型: 1.全局变量 ...
- Linux下安装docker(1)
1.由于centos系统已经自带docker源了,所以可以直接安装: yum install docker 如果是centos6.5版本的,使用yum -y install docker-io 进行安 ...
- 来源于知乎专栏:https://zhuanlan.zhihu.com/p/29619457
1. 校验数字的表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和非零开头的数字:^(0|[1- ...
- springboot学习二:配置文件配置
springboot默认读取application*.properties #######spring配置####### spring.profiles.active=dev //引入开发配置文件 a ...
- (Python基础)集合操作
集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 以下代码演示了去重,增删改查,以及关系测试供参考学习 ...
- Mac端StartUML的安装和破解
**本人安装的是StarUML-3.0.1版本** 一.下载与安装 1. 从官方网站下载,网址:http://staruml.io/ 2. dmg文件下载完成后,双击安装. 二.破解 1. 安装npm ...
- 一条SQL语句执行得很慢的原因有哪些?
说实话,这个问题可以涉及到 MySQL 的很多核心知识,可以扯出一大堆,就像要考你计算机网络的知识时,问你“输入URL回车之后,究竟发生了什么”一样,看看你能说出多少了. 之前腾讯面试的实话,也问到这 ...