POJ 3159 Candies(差分约束,最短路)
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 20067 | Accepted: 5293 |
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
//============================================================================
// Name : POJ.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
/*
* 使用优先队列优化Dijkstra算法
* 复杂度O(ElogE)
* 注意对vector<Edge>E[MAXN]进行初始化后加边
*/
const int INF=0x3f3f3f3f;
const int MAXN=;
struct qnode
{
int v;
int c;
qnode(int _v=,int _c=):v(_v),c(_c){}
bool operator <(const qnode &r)const
{
return c>r.c;
}
};
struct Edge
{
int v,cost;
int next;
};
Edge edge[];
int tol;
int head[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n,int start)//点的编号从1开始
{
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty())
{
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
int cost=edge[i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u,int v,int w)
{
edge[tol].v=v;
edge[tol].cost=w;
edge[tol].next=head[u];
head[u]=tol++;
} int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m)==)
{
tol=;
memset(head,-,sizeof(head));
int A,B,C;
while(m--)
{
scanf("%d%d%d",&A,&B,&C);
addedge(A,B,C);
}
Dijkstra(n,);
printf("%d\n",dist[n]);
}
return ;
}
POJ 3159 Candies(差分约束,最短路)的更多相关文章
- 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 ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- 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
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
随机推荐
- [HDOJ2586]How far away?(最近公共祖先, 离线tarjan, 并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 这题以前做过…现在用tarjan搞一发…竟然比以前暴力过的慢………… 由于是离线算法,需要Que ...
- Android 中Activity生命周期分析(二):从AActivity 到BActivity过程分析
如果你没有动手去演示的话,你一定要去动手试试看,这个东西非学容易出错,面试中经常出现,好了,上代码: package com.king.review.base; import android.app. ...
- fiddler for mac
Fiddler 是一免费的web调试工具.并且兼容所有浏览器.系统和平台. Fiddler 是基于微软的 .Net 技术开发的,没办法直接在 Mac/Linux 下使用.本文介绍一些替代方案(这些方案 ...
- Web内容管理系统 Magnolia 介绍-挖掘优良的架构(1)
Magnolia简介: Magnolia CMS是一家瑞士公司自2003年起发布的一个基于Java的开源内容管理系统.它适合且已被使用在以下领域:电子商务(例如:COOP.Migros.Rossman ...
- oracle判断一个字符串中是否包含另外一个字符串
select * from a where instr(a,b)>0; 用于实现B字段是A字段中的某一部分的时候,要论顺序或者要相邻的字符. 如果想要不论顺序或者不相邻的字符时,定义函数可以实现 ...
- CodeForces Round #290 Fox And Dinner
而是Div2的最后一题,当时打比赛的时候还不会最大流.自己能够把它写出来然后1A还是很开心的. 题意: 有n个不小于2的整数,现在要把他们分成若干个圈.在每个圈中,数字的个数不少于3个,而且相邻的两个 ...
- 手动配置gradle
最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...
- codevs 1135 选择客栈
这题没什么话说. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- [反汇编练习] 160个CrackMe之020
[反汇编练习] 160个CrackMe之020. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 解决jQuery对表单serialize后出现的乱码问题
通过看jQuery源码可以知道,serialize方法是通过encodeURIComponent编码的,所以解决乱码的最笨方法: 1.重新分解序列化后的值 2.把分解的值重新decodeURICo ...