poj3159 Candies(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Time Limit: 1500MS | Memory Limit: 131072K |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <cstdlib>
using namespace std;
#define REP(A,X) for(int A=0;A<X;A++)
#define MAXE 200010
#define MAXP 30010
#define INF 0x7fffffff
#define MP(A,B) make_pair(A,B)
typedef pair<int,int> PII ;
struct node{
int v,d,next;
}edge[MAXE];
int e=;
int head[MAXP];
void init(){
e=;
REP(i,MAXP)head[i]=-;
}
void add_edge(int u,int v,int d)
{
edge[e].v=v;
edge[e].d=d;
edge[e].next=head[u];
head[u]=e;
e++;
}
int vis[MAXP],dis[MAXP];
void spfa()
{
REP(i,MAXP)vis[i]=;
REP(i,MAXP)dis[i]=i==?:INF;
//queue<int>q;
stack<int>q;
q.push();
vis[]=;
while(!q.empty()){
//int x=q.front();
int x=q.top();
q.pop();
for(int t=head[x];t!=-;t=edge[t].next)
{
int y=edge[t].v;
int d=edge[t].d;
if(dis[y]>dis[x]+d){
dis[y]=dis[x]+d;
if(!vis[y]){
q.push(y);
vis[y]=;
}
}
}
vis[x]=;
}
}
void dijkstra(int s)
{
REP(i,MAXP)vis[i]=;
REP(i,MAXP)dis[i]=i==s?:INF;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push(MP(dis[s],s));
while(!q.empty())
{
PII p=q.top();
q.pop();
int x=p.second;
if(vis[x])continue;
vis[x]=;
for(int t=head[x];t!=-;t=edge[t].next)
{
int y=edge[t].v;
int d=edge[t].d;
if(!vis[y]&&dis[y]>dis[x]+d)
{
dis[y]=dis[x]+d;
q.push(MP(dis[y],y));
}
}
} } int main()
{
int m,n;
while(scanf("%d%d",&n,&m)!=EOF){
int u,v,w;
init();
REP(i,m){
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
//dijkstra(1);
spfa();
printf("%d\n",dis[n]);
}
return ;
}
代码君
poj3159 Candies(差分约束)的更多相关文章
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- [poj3159]Candies(差分约束+链式前向星dijkstra模板)
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...
- POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS Memory Limit: 131072K Total Submiss ...
- POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)
Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include< ...
- 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[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- poj 3159 Candies 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ3159(KB4-K 差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 33283 Accepted: 9334 Descrip ...
随机推荐
- 把python项目部署到centos里
.安装centos VMware9下面安装centos .在centos下面设置共享文件夹为你本地的论坛的代码,然后设置网络为桥接:直接连接到物理网络,赋值网络连接状态 .进入forum_svr.py ...
- jar打包命令
jar查看jar命令的使用 1. jar cvf 生成jar包的完整名称 要生成的class文件所在目录以及名称 jar -cvf aa.jar *.* 2.将jar包放到环境变量中,可以不用把cla ...
- angularjs directive 实例 详解
前面提到了angularjs的factory,service,provider,这个可以理解成php的model,这种model是不带html的,今天所说的directive,也可以理解成php的mo ...
- PHP & JAVA 实现 PBKDF2 加密算法
PHP代码: /** * PBKDF2 加密函数 * 参考标准 * @link https://www.ietf.org/rfc/rfc2898.txt * * php官方函数将在php5.5发布 * ...
- C语言之利用递归将十进制转换为二进制
#include<stdio.h>#include<stdlib.h>void change2(int num){ if (num != 0) { change2(n ...
- 新闻动态切换图片html(flash)
效果图: 代码: <table id="table_zi"> <tr> <td class="width330"> < ...
- js代码中的parent,top和self有什么区别
.parent常用在iframe和frame中的子页面访问父页面中的对象 .top :一个页面可能会有很多层,top是指最顶层的框架 .self :是指当前窗口
- PowerShell官方的MSDN
https://msdn.microsoft.com/en-us/powershell/mt173057.aspx 官方还咋github上放置了 扩展模块. 比如 web iis部署.sqlserv ...
- Eclipse配置JDK的源代码的src.zip
Eclipse配置JDK的源代码的src.zip包很简单.只需要简单的几个步骤. 1.点 “window”-> “Preferences” -> “Java” -> “Install ...
- ssh添加公钥
ssh-keygen生成公钥存储在文件: ~/.ssh/id_rsa.pub 如果ssh-add -l命令后没有一串长的字符串, 把私钥密钥对的ID(fingerPrint)加入ssh的认证代理ssh ...