zoukankan      html  css  js  c++  java
  • BNUOJ 3278 Candies

    Candies

    Time Limit: 1500ms
    Memory Limit: 131072KB
    This problem will be judged on PKU. Original ID: 3159
    64-bit integer IO format: %lld      Java class name: Main
     
     

    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

    Source

     
    解题:一个很裸的差分约束问题!直接求1到n的最短路
     
    至于为什么spfa要用栈而不是队列,因为一直超时!vector没法用,用了就超时!
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 using namespace std;
    16 struct arc{
    17     int to,w,next;
    18 };
    19 arc g[200000];
    20 int head[30010],tot,n,m;
    21 int d[30010];
    22 bool done[30010];
    23 void add(int u,int v,int w){
    24     g[tot].to = v;
    25     g[tot].w = w;
    26     g[tot].next = head[u];
    27     head[u] = tot++;
    28 }
    29 stack<int>stk;
    30 void spfa(int s){
    31     d[s] = 0;
    32     while(!stk.empty()) stk.pop();
    33     done[s] = true;
    34     stk.push(s);
    35     int u,i;
    36     while(!stk.empty()){
    37         u = stk.top();
    38         stk.pop();
    39         done[u] = false;
    40         for(i = head[u]; i != -1; i = g[i].next){
    41             if(d[g[i].to] > d[u] + g[i].w){
    42                 d[g[i].to] = d[u] + g[i].w;
    43                 if(!done[g[i].to]){
    44                     done[g[i].to] = true;
    45                     stk.push(g[i].to);
    46                 }
    47             }
    48         }
    49     }
    50 }
    51 int main(){
    52     int i,j,u,v,w;
    53     while(~scanf("%d %d",&n,&m)){
    54         for(i = 0; i <= n; i++){
    55             done[i] = false;
    56             head[i] = -1;
    57             d[i] = INF;
    58         }
    59         for(tot = i = 0; i < m; i++){
    60             scanf("%d %d %d",&u,&v,&w);
    61             add(u,v,w);
    62         }
    63         spfa(1);
    64         printf("%d
    ",d[n]);
    65     }
    66     return 0;
    67 }
    View Code

    好吧,加个优先队列版的Dijkstra吧

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 #define pii pair<int,int>
    16 using namespace std;
    17 struct arc{
    18     int to,w,next;
    19 };
    20 arc g[200000];
    21 int head[30010],d[30010],tot,n,m;
    22 bool done[30010];
    23 void add(int u,int v,int w){
    24     g[tot].to = v;
    25     g[tot].w = w;
    26     g[tot].next = head[u];
    27     head[u] = tot++;
    28 }
    29 priority_queue<pii,vector< pii >,greater< pii > >q;
    30 void dijkstra(int s,int t){
    31     while(!q.empty()) q.pop();
    32     d[s] = 0;
    33     q.push(make_pair(d[s],s));
    34     while(!q.empty()){
    35         int u = q.top().second;
    36         q.pop();
    37         if(done[u]) continue;
    38         done[u] = true;
    39         for(int i = head[u]; i != -1; i = g[i].next){
    40             if(d[g[i].to] > d[u] + g[i].w){
    41                 d[g[i].to] = d[u] + g[i].w;
    42                 q.push(make_pair(d[g[i].to],g[i].to));
    43             }
    44         }
    45         if(done[t]) return;
    46     }
    47 }
    48 int main(){
    49     int i,u,v,w;
    50     while(~scanf("%d %d",&n,&m)){
    51         for(i = 0; i <= n; i++){
    52             head[i] = -1;
    53             done[i] = false;
    54             d[i] = INF;
    55         }
    56         for(tot = i = 0; i < m; i++){
    57             scanf("%d %d %d",&u,&v,&w);
    58             add(u,v,w);
    59         }
    60         dijkstra(1,n);
    61         printf("%d
    ",d[n]);
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    NFC
    精品收藏:GitHub人工智能AI开源项目
    typedef的用法,C语言typedef详解
    “此人不存在”
    视频换脸-Deepfakes代码解读和训练说明
    Linux下__attribute__((visibility ("default")))的使用
    piao
    高通开源android源码下载
    iOpenWorskSDK下载和答疑贴
    解构领域驱动设计(三):领域驱动设计
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3906487.html
Copyright © 2011-2022 走看看