题目链接:http://poj.org/problem?id=3159

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   and   respectively. N is the number of kids in the class and the kids were numbered  through N. snoopy and flymouse were always numbered  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

Sample Output

题意:flymouse是幼稚园班上的班长,一天老师给小朋友们买了一堆的糖果,由flymouse来分发。flymouse希望自己分得的糖果数尽量多于snoopy。对于其他小朋友而言,则必须自己得到的糖果不少于班上某某,给出m个这种约束关系(u,v, w)即同学u的糖果数不能比同学v的糖果数少w。问flymouse最多能多snoopy几个糖果。

方法:用spfa写,开始用队列存,提交超时了,然后用堆栈存,测试数据问题

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 30010
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
struct node
{
int v,l,next; }s[];
int Map[N],vis[N],dis[N];
int k,n,q[N];
void add(int e,int f,int l)
{
s[k].l=l;
s[k].next=Map[e];
s[k].v=f;
Map[e]=k++;
}
void spfa()
{
int t=;
met(dis,);
for(int i=;i<=n;i++)
dis[i]=INF;
q[++t]=;///用堆栈存
vis[]=;
dis[]=;
while(t)
{
int u=q[t--];
vis[u]=;
for(int i=Map[u];i!=-;i=s[i].next)
{
int v=s[i].v;
if(dis[v]>dis[u]+s[i].l)
{
dis[v]=dis[u]+s[i].l;
if(!vis[v])
{
q[++t]=v;
vis[v]=;
}
}
}
}
}
int main()
{
int m,e,f,l;
while(scanf("%d %d",&n,&m)!=EOF)
{
met(Map,-);k=;
for(int i=;i<m;i++)
{
scanf("%d %d %d",&e,&f,&l);
add(e,f,l);
}
spfa();
printf("%d\n",dis[n]);
}
return ;
}

(poj)3159 Candies的更多相关文章

  1. POJ——T 3159 Candies

    http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   ...

  2. (poj)1502 MPI Maelstrom

    题目链接:http://poj.org/problem?id=1502 Description BIT has recently taken delivery of their processor A ...

  3. (poj)1806 Currency Exchange

    题目链接:http://poj.org/problem?id=1860 Description Several currency exchange points are working in our ...

  4. (poj)3268 Silver Cow Party 最短路

    Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...

  5. (poj)3020 Antenna Placement 匹配

    题目链接 : http://poj.org/problem?id=3020 Description The Global Aerial Research Centre has been allotte ...

  6. (poj)1064 Cable master 二分+精度

    题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...

  7. (poj)1679 The Unique MST 求最小生成树是否唯一 (求次小生成树与最小生成树是否一样)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  8. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. Intersection(poj)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13140   Accepted: 3424 Des ...

随机推荐

  1. c# 泛型List的定义、作用、用法

    定义:List<T>类表示可通过索引访问的对象的强类型列表,提供用于对列表进行搜索.排序和操作的方法. 作用: 泛型最常见的用途是泛型集合 我们在创建列表类时,列表项的数据类型可能是int ...

  2. Smack[3]用户列表,头像,组操作,用户操作

    用户列表 Smack主要使用Roster进行列表管理的 connection.getRoster(); /** * 返回所有组信息 <RosterGroup> * * @return Li ...

  3. SVN 中trunk、branches、tags都什么意思?

    1.trunk是主分支,是日常开发进行的地方. 2.branches是分支.一些阶段性的release版本,这些版本是可以继续进行开发和维护的,则放在branches目录中.又比如为不同用户客制化的版 ...

  4. ThinkPHP Volist标签

    Volist标签主要用于在模板中循环输出数据集或者多维数组. volist标签(循环输出数据) 闭合 非闭合标签 属性 name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可 ...

  5. css Hack,用IE11模拟测试的,条件注释要找真IE去测,模拟的无效

    <!DOCTYPE html> <!--[if lt IE 7 ]> <html class="ie6 ie"> <![endif]--& ...

  6. linux 学习笔记 Linux内核的四大功能简介

    一 内存管理 默认情况下,运行在Linux系统上的每个进程都有各自的内存页面:进程不能访问其他进程正在使用的内存页面. 内核维护着自己的内存区域 用户进程不能访问内核进程的内存 可以创建一些共享内存页 ...

  7. GCD多线程

    GCD本质线程自动管理指令包 GCD优点: 1.GCD 本身自带有线程锁的效果,能通过推迟昂贵计算任务并在后台运行它们来改善应用的响应性能. 2.GCD 提供了更易于使用的并发模型(效果方面类似于对锁 ...

  8. 关于jQuery源码中(function(window,undefined){//dosomething()})(window)写法解释

    一.首先是最常见的闭包 (Closure) 范式自执行函数的写法,这里用匿名函数封装(构造块级作用域),避免了匿名函数内部的代码与外部之间发生冲突(如使用了相同的变量名). (function() { ...

  9. 关于SpringAOP的XML方式的配置

    AOP(XML)[理解][应用][重点] 1.AOP基础实例 A.导入jar包 核心包(4个)         日志(2个)             AOP(4个) Spring进行AOP开发(1个) ...

  10. 【转】Android应用程序的数据存放目录解说

    Android的每个应用程序,都有自己的可控的目录. 在Setting/Application info里面,可以看到每个应用程序,都有Clear data和Clear cache选项. 具体这些目录 ...