• poj Candies


    这题不知道说什么了,不过是个好题
    先自己写差分约束,然后WA了……discuss里说超时也显示WA  - -|||,c++交也是TLE……尴尬
    还有一个重点, 就是我在discuss里看到的。
    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
    
    因为是班长做主,所以程序必须是从1号为起点,否则如果以n为起点,那就是爱管闲事的那人做主。(具体原因你可想想单源最短路的更新原理)
    再举个简单的样例:
    2
    1 2 5
    2 1 6
    如果以1为起点得到的答案是5,也就是班长5个糖果,爱管闲事的0个糖果(当然两者可以同时
    加上任意的正整数)。如果以2为起点,得到的答案是6,也就是班长得0个,爱管闲事的人得
    6个。虽然6比5大,但是根据题意,分配糖果是班长做主,而他是想惩戒爱管闲事者,所以他肯
    定会按第一种分配方案分配,所以答案仍旧是5,所以必须以1号为起点。
    以下是参考某个blog改的,头的注释是copy的,真的是该queue为手写堆就过了,希望有大牛告诉我什么时候spfa+bfs效果
    会比spfa+dfs好呢,本来我一直觉得bfs是最快的,现在有点迷茫了

    /*
    给n个人派糖果,给出m组数据,每组数据包含A,B,c  三个数,
    意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。
    最后求n 比 1 最多多多少糖果。
    【解题思路】
    这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,
    我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术,
    即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。
    即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大,
    所以这题可以转化为最短路来求。
    这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用 spfa + stack

    2011-7-7
    */

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <queue>
    #include <climits>
    #define INF INT_MAX
    #define nMax 30003
    #define mMax 150005
    using namespace std;
    int m, n, cc;
    int head[nMax];
    int sta[nMax];
    bool vis[nMax];
    int dist[nMax];
    struct Node
    {
    int v, w, next;
    } node[mMax];

    void BuildGraph(int a, int b, int c)
    {
    node[cc].v=b;
    node[cc].w=c;
    node[cc].next=head[a];
    head[a]=cc++;
    }

    void spfa()
    {
    int top=0;
    memset(vis, 0, sizeof(vis));
    for(int i=2; i<=n; i++) dist[i]=INF;
    dist[1]=0;

    sta[top]=1;
    top++;
    while(top)
    {
    int x=sta[top-1];
    top--;
    vis[x]=0;

    for(int i=head[x]; i!=-1; i=node[i].next)
    {
    if(dist[x]+node[i].w<dist[node[i].v])
    {
    dist[node[i].v]=dist[x]+node[i].w;
    if(!vis[node[i].v])
    {
    sta[top++]=node[i].v;
    vis[node[i].v]=1;
    }
    }
    }
    }
    printf("%d\n", dist[n]);
    }

    int main()
    {
    while(scanf("%d%d", &n, &m)!=EOF)
    {
    cc=0;
    memset(head, -1, sizeof(head));
    for(int i=0; i<m; i++)
    {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    BuildGraph(a, b, c);
    }
    spfa();
    }
    return 0;
    }
    作者:FreeAquar
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    KETTLE封装
    基于MODBUS-RTU协议的串口编程
    阿里DRUID 配置说明及分析
    CopyOnWriteArrayList集合排序异常问题
    CopyOnWriteArrayList集合线程安全解释
    JAR包数字签名与验证
    MySQL中select * for update锁表的范围
    Kettle文本文件输出和输入控件使用中,换行符导致的问题处理
    UAP如何根据DeviceFamily显示不同的页面
    Windows 10 响应式设计和设备友好的开发
  • 原文地址:https://www.cnblogs.com/FreeAquar/p/2100575.html
Copyright © 2020-2023  润新知