• POJ3159(KB4-K 差分约束)


    Candies

    Time Limit: 1500MS   Memory Limit: 131072K
    Total Submissions: 33283   Accepted: 9334

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

     
     1 //2017-08-29
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <stack>
     8 
     9 using namespace std;
    10 
    11 const int N = 50010;
    12 const int M = 250010;
    13 const int INF = 0x3f3f3f3f;
    14 
    15 int head[N], tot;
    16 struct Edge{
    17     int to, next, w;
    18 }edge[M];
    19 
    20 void init(){
    21     tot = 0;
    22     memset(head, -1, sizeof(head));
    23 }
    24 
    25 void add_edge(int u, int v, int w){
    26     edge[tot].w = w;
    27     edge[tot].to = v;
    28     edge[tot].next = head[u];
    29     head[u] = tot++;
    30 }
    31 
    32 int n, m;
    33 bool vis[N];
    34 int dis[N], cnt[N];
    35 int sk[N], top;//手写栈
    36 bool spfa(int s, int n){
    37     for(int i = 1; i <= n; i++){
    38         vis[i] = 0;
    39         cnt[i] = 0;
    40         dis[i] = INF;
    41     }
    42     vis[s] = 1;
    43     dis[s] = 0;
    44     cnt[s] = 1;
    45     top = 0;
    46     sk[top++] = s;
    47     while(top){
    48         int u = sk[--top];
    49         vis[u] = 0;
    50         for(int i = head[u]; i != -1; i = edge[i].next){
    51             int v = edge[i].to;
    52             if(dis[v] > dis[u] + edge[i].w){
    53                 dis[v] = dis[u] + edge[i].w;
    54                 if(!vis[v]){
    55                     vis[v] = 1;
    56                     sk[top++] = v;
    57                     if(++cnt[v] > n)return false;
    58                 }
    59             }
    60         }
    61     }
    62     return true;
    63 }
    64 
    65 int main()
    66 {
    67     //freopen("inputK.txt", "r", stdin);
    68     while(scanf("%d%d", &n, &m)!=EOF){
    69         init();
    70         int u, v, w;
    71         while(m--){
    72             scanf("%d%d%d", &u, &v, &w);
    73             add_edge(u, v, w);
    74         }
    75         spfa(1, n);
    76         printf("%d
    ", dis[n]);
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    PHP获取一周的日期
    关系型数据库和非关系型数据库的区别和特点
    关系型数据库和非关系型数据库的特性以及各自的优缺点
    什么是数据库?什么是关系数据库?什么是非关系型数据库?
    PHP中把对象转数组的几个方法
    验证银行卡号
    xss过滤方法
    PHP 随机字符
    计算两个日期相差年月日
    判断一个时间段是否包含周末
  • 原文地址:https://www.cnblogs.com/Penn000/p/7447839.html
Copyright © 2020-2023  润新知