zoukankan      html  css  js  c++  java
  • poj3159 Candies

    地址:http://poj.org/problem?id=3159

    题目:

    Candies
    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 31463   Accepted: 8782

    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 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

    32-bit signed integer type is capable of doing all arithmetic.

    Source

     
    思路:
      差分约束:对于每个信息 x y z,建一条从x到y的权值为z的边。然后跑最短路。
      别用queue,会T。要用stack。
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cmath>
     4 
     5 using namespace std;
     6 
     7 #define MP make_pair
     8 #define PB push_back
     9 typedef long long LL;
    10 typedef pair<int,int> PII;
    11 const double eps=1e-8;
    12 const double pi=acos(-1.0);
    13 const int K=1e6+7;
    14 const int mod=1e9+7;
    15 
    16 struct node
    17 {
    18     int to,v,next;
    19 }edge[K];
    20 int tot,head[K];
    21 void add(int x,int y,int z)
    22 {
    23     edge[tot].to=y,edge[tot].v=z,edge[tot].next=head[x];
    24     head[x]=tot++;
    25 }
    26 int vis[K],dis[K],sk[K];
    27 void spfa(void)
    28 {
    29     int cnt=0;
    30     sk[cnt++]=1,vis[1]=1,dis[1]=0;
    31     while(cnt)
    32     {
    33         int u=sk[--cnt];
    34         vis[u]=0;
    35         for(int i=head[u];~i;i=edge[i].next)
    36         {
    37             int v=edge[i].to,w=edge[i].v;
    38             if(dis[v]>dis[u]+w)
    39             {
    40                 dis[v]=dis[u]+w;
    41                 if(!vis[v])
    42                     sk[cnt++]=v,vis[v]=1;
    43             }
    44         }
    45     }
    46 }
    47 int main(void)
    48 {
    49     int n,m;
    50     while(~scanf("%d%d",&n,&m))
    51     {
    52         tot=0;
    53         for(int i=1;i<=n;i++)
    54             head[i]=-1,vis[i]=0,dis[i]=0x3f3f3f3f;
    55         for(int i=1,u,v,w;i<=m;i++)
    56             scanf("%d%d%d",&u,&v,&w),add(u,v,w);
    57         spfa();
    58         printf("%d
    ",dis[n]);
    59     }
    60     return 0;
    61 }

     

  • 相关阅读:
    11->centos6 安装oracle
    centos7安装rlwrap
    ajax
    java获取时间戳
    idea主要设置大纲图
    ppycharm设置解释器版本号码
    JTA 深度历险
    学会数据库读写分离、分表分库——用Mycat,这一篇就够了!
    分库分表的几种常见玩法及如何解决跨库查询等问题
    深入浅出SOA
  • 原文地址:https://www.cnblogs.com/weeping/p/6973408.html
Copyright © 2011-2022 走看看