Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 27051   Accepted: 7454

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 AB and c in order, meaning that kid A believed that kid B should never get overc 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
题意:给出N个孩子,再给出M个限制。A,B,c表示孩子B的糖果最多比孩子A的糖果多c个。问N号孩子最多比1号孩子多多少的糖果。
思路:转化为求1号结点到N号结点的最短路问题。
/*
dijkstra Accepted 3112KB 547ms G++
*/
#include"cstdio"
#include"cstring"
#include"queue"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
struct P{
int fi,se;
P(int cfi,int cse):fi(cfi),se(cse){}
bool operator<(const P& a) const
{
return fi > a.fi;
}
};
int heap[MAXN];
int V,E;
void add_edge(int u,int v,int co)
{
es[E].to=v;
es[E].cost=co;
es[E].next=heap[u];
heap[u]=E;
E++;
}
int d[MAXN];
int dijkstra(int s)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P> que;
que.push(P(,s));
d[s]=;
while(!que.empty())
{
P p=que.top();que.pop();
int v=p.se;
if(d[v]<p.fi) continue;
for(int i=heap[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[v]+e.cost)
{ d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
return d[V];
}
int main()
{ int N,M;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(heap,-,sizeof(heap));
V=N,E=;
for(int i=;i<M;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
}
int ans=dijkstra();
printf("%d\n",ans);
} return ;
}

下面是用栈实现的spfa算法。用队列实现会TLE。

/*
spfa Accepted 3112KB 547ms G++
*/
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int to,cost,next;
}es[MAXN];
int stack[MAXN],top;
int head[MAXN];
int V,E;
void add_edge(int u,int v,int co)
{
es[E].to=v;
es[E].cost=co;
es[E].next=head[u];
head[u]=E;
E++;
}
int d[MAXN];
int vis[MAXN];
int spfa(int s)
{
for(int i=;i<=V;i++) d[i]=INF;
memset(vis,,sizeof(vis));
top=;
stack[top++]=s;
vis[]=s,d[s]=; while(top!=)
{
int v=stack[--top];
vis[v]=;
for(int i=head[v];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
if(!vis[e.to])
{
vis[e.to]=;
stack[top++]=e.to;
}
}
}
}
return d[V];
}
int main()
{ int N,M;
while(scanf("%d%d",&N,&M)!=EOF)
{
memset(head,-,sizeof(head));
V=N,E=;
for(int i=;i<M;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co);
}
int ans=spfa();
printf("%d\n",ans);
} return ;
}

POJ3159(最短路)的更多相关文章

  1. poj3159 最短路(差分约束)

    题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...

  2. poj3159最短路spfa+邻接表

    https://vjudge.net/contest/66569#problem/K 相当于模板吧,第一次写spfa的 #include<iostream> #include<cst ...

  3. POJ-3159 Candies 最短路应用(差分约束)

    题目链接:https://cn.vjudge.net/problem/POJ-3159 题意 给出一组不等式 求第一个变量和最后一个变量可能的最大差值 数据保证有解 思路 一个不等式a-b<=c ...

  4. 【poj3159】 Candies

    http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1 ...

  5. poj3159 Candies(差分约束,dij+heap)

    poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...

  6. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  7. bzoj1001--最大流转最短路

    http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...

  8. 【USACO 3.2】Sweet Butter(最短路)

    题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...

  9. Sicily 1031: Campus (最短路)

    这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...

随机推荐

  1. FAT12中,如何定位大于一个扇区(512B)的文件内容

    [0]README 0.1)本文旨在于 演示在FAT12中, 如何取定位大于 512B 的文件内容,和查看它: 0.2)如何制作FAT12文件系统,以及如何向文件中添加temp.txt文件,参见: { ...

  2. 模式识别开发之项目---基于opencv的手势识别

    我使用OpenCV2.4.4的windows版本+Qt4.8.3+VS2010的编译器做了一个手势识别的小程序. 本程序主要使到了Opencv的特征训练库和最基本的图像处理的知识,包括肤色检测等等. ...

  3. 用Delphi实现网络视频编程

    在MSN.QQ等聊天类的应用程序中,都应用到了网络视频技术.Delphi使用Object Pascal语言是一种完全面向对象语言,可以开发出灵活强大的程序,开发网络视频程序也不在话下.一个完整的网络视 ...

  4. EasyNVR RTSP摄像机转HLS直播服务器中使用Onvif协议控制预置位

    EasyNVR支持预置位控制,包括转到指定预置位,设置指定预置位,删除指定预置位 预置位在安防领域有较为普遍的应用,可以进行很多既定位置的跳转,很方便 之前我们说过如何用Onvif协议进行设备的发现, ...

  5. dubbo介绍及其使用案例

    dubbo介绍及其使用案例 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果 ...

  6. 九度OJ 1078:二叉树遍历 (二叉树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3748 解决:2263 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树 ...

  7. 子串的索引 str.index(sub) sub必须存在

    ii.lstrip(' ')[0:2]=='//' ii.lstrip(' ').index('//')==0

  8. MaLoc: a practical magnetic fingerprinting approach to indoor localization using smartphones

    https://www.indooratlas.com/ MaLoc: a practical magnetic fingerprinting approach to indoor localizat ...

  9. 设计模式 - 单件模式(singleton pattern) 具体解释

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/28595349 单件模式(singleton ...

  10. 使用了Tomcat JDBC连接池不能重连的问题

    在项目中用到了tomcat 的jdbc连接池,发现一个问题是,当数据库重启时,服务没有重新的去连接数据库,需要将部署的项目重新启动才能连接到数据库.经过测试对配置做一下修改: 在配置dataSourc ...