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,问可能的最大距离.如果没 ...
随机推荐
- Java程序国际化学习代码一
Java程序国际化初识 1.基本思路 Java程序的国际化的思路是将程序中的标签.提示等信息放在资源文件中,程序需要支持哪些国家.语言环境,就对应提供相应的资源文件.资源文件是key-value对,每 ...
- Windows下安装MySQL8
转自:https://blog.csdn.net/star_in_shy/article/details/82691330 感谢! 一.MySQL官网下载 (一)MySQL下载地址:https:// ...
- yum搭建 Lamp环境
yum搭建Lamp yum install -y httpd yum install -y nano rpm 安装 Php7 相应的 yum源 rpm -Uvh https://dl.fedorapr ...
- golang初识 - install go on ubuntu
WSL: Ubuntu 18.04 1. install go (1) unzip sudo mkdir -p /usr/local/go sudo tar zxvf go1.12.4.linux-a ...
- MySQL实现分组取组内特定数据的功能
需求:在MySQL5.7环境下,查询下面表中,各个学科前两名的学生的成绩: 1.准备数据 窗机表以及向表中插入数据 创建一张表: DROP TABLE IF EXISTS `grade`; CREAT ...
- 将自己的SpringBoot应用打包发布到Linux下Docker中
目录 将自己的SpringBoot应用打包发布到Linux下Docker中 1. 环境介绍 2. 开始前的准备 2.1 开启docker远程连接 2.2 新建SpringBoot项目 3. 开始构建我 ...
- djiango 虚拟环境与项目创建
建立虚拟环境 一,查看有那些虚拟环境 :workon 二,创建虚拟环境:mkvirtualenv -p/usr/bin/python3 django(p后面是路径) 三,进入虚拟环境:workon d ...
- 单片机课程设计——课程设计之四位加法计算器(2)(C代码)
#include<reg52.h> typedef unsigned char uint8; typedef unsigned int uint16; sbit rw=P2^5; sbit ...
- Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements
Caused by: javax.persistence.NonUniqueResultException: result returns more than one elements at org. ...
- python学习笔记----random
import random import string # 随机整数: print random.randint(1,50) >>> print(random.randint(1,5 ...