zoukankan      html  css  js  c++  java
  • Candies POJ

    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

    32-bit signed integer type is capable of doing all arithmetic.
     
    题意:求1到n的最大路径
    思路:将每个人允许的最大糖果差当作路径,然后求最短路,因为到每个点,要满足每个人到这个点的最大糖果差,所以选择最小的糖果数肯定满足所有情况
    (差分约束?)
     1 #include<cstdio>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 int dist[30005];
     7 bool vis[30005];
     8 
     9 typedef pair<int,int>pii;
    10 struct Node
    11 {
    12     int y,val,next;
    13 }node[150005];
    14 
    15 int cnt,head[30005];
    16 
    17 void add(int u,int v,int val)
    18 {
    19     node[++cnt].y=v;
    20     node[cnt].val=val;
    21     node[cnt].next=head[u];
    22     head[u]=cnt;
    23 }
    24 
    25 int n,m;
    26 priority_queue<pii,vector<pii>,greater<pii> >que;
    27 void dijstra()
    28 {
    29     while(!que.empty())que.pop();
    30     que.push(pii(0,1));
    31     memset(dist,0,sizeof(dist));
    32     while(!que.empty())
    33     {
    34         pii tmp = que.top();
    35         que.pop();
    36         int s=tmp.second;
    37         int v=tmp.first;
    38         if(vis[s])continue;
    39         vis[s]=1;
    40         dist[s]=v;
    41         for(int i=head[s];i;i=node[i].next)
    42         {
    43             int to = node[i].y;
    44             if(dist[to] < v+node[i].val)
    45             {
    46                 que.push(pii(v+node[i].val,to));
    47             }
    48         }
    49     }
    50 }
    51 
    52 int main()
    53 {
    54     scanf("%d%d",&n,&m);
    55     for(int i=1;i<=m;i++)
    56     {
    57         int u,v,k;
    58         scanf("%d%d%d",&u,&v,&k);
    59         add(u,v,k);
    60     }
    61     dijstra();
    62     printf("%d
    ",dist[n]);
    63 }
    View Code
     
  • 相关阅读:
    js键盘事件
    jq 插件写法
    js 去除字符串空白符
    C# ExpandoObject用法
    C# dynamic与var的区别
    c# 可选参数与命名实参
    C# 扩展方法
    python运算符
    【HDOJ】3006 The Number of set
    【HDOJ】3205 Factorization
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10772583.html
Copyright © 2011-2022 走看看