zoukankan      html  css  js  c++  java
  • HDU——2647Reward(DFS或差分约束)

    Reward

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7078    Accepted Submission(s): 2205

    Problem Description
    Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
    The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.
     
    Input
    One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
    then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
     
    Output
    For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
     
    Sample Input
    2 1 1 2 2 2 1 2 2 1
     
    Sample Output
    1777 -1
     
    Author
    dandelion
     

    晚上无聊随便看看,然后发现了这题,网上说可以用拓扑排序,画了个草图,发现拓扑并不好用,而且感觉可能排出来会错,然后就自己想了个DFS,感觉这题用DFS还是满靠谱的,有一个坑点就是可能存在部分成环,即有一部分点正常而另一部分是环,因此WA两次(以为DFS写错了检查半天……)DFS和SPFA时间差不多都是40MS左右。对于图的DFS和BFS遍历还是比较好写的。SPFA判负环就还是用的入度数组,就没用访问次数数组了。

    DFS代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<sstream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<deque>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define MM(x) memset(x,0,sizeof(x))
    #define MMINF(x) memset(x,INF,sizeof(x))
    typedef long long LL;
    const double PI=acos(-1.0);
    const int M=20010,N=10010;
    struct info
    {
        int to;
        int pre;
    }E[M];
    int head[N],cnt;
    int deg[N];
    int level[N],vis[N];
    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=0;
        MM(deg);
        MM(level);
    }
    void add(int s,int t)
    {
        E[cnt].to=t;
        E[cnt].pre=head[s];
        head[s]=cnt++;
    }
    void dfs(int s,int tl)
    {
        vis[s]=1;
        for (int i=head[s]; i!=-1; i=E[i].pre)
        {
            int v=E[i].to;
            if(!vis[v])
            {
                cnt++;
                level[v]=max(level[v],tl+1);
                dfs(v,tl+1);
            }
        }
        vis[s]=0;
    }
    queue<int>Q;
    int main(void)
    {
        int n,m,i,j,a,b,c;
        while (~scanf("%d%d",&n,&m))
        {
            init();
            for (i=0; i<m; i++)
            {
                scanf("%d%d",&a,&b);
                add(b,a);
                deg[a]++;
            }
            while (!Q.empty())
            	Q.pop();
            int C=0;
            for (i=1; i<=n; i++)
            {
                if(!deg[i])
                {
                    Q.push(i);
                    C++;
                }
            }
            while (!Q.empty())
            {
                int now=Q.front();
                Q.pop();
                dfs(now,0);
            }
            for (i=1; i<=n; i++)
            {
                if(!level[i])
                    C--;
            }
            if(C)
                puts("-1");
            else
            {
                int r=0;
                for (i=1; i<=n; i++)
                    r+=888+level[i];
                printf("%d
    ",r);
            }
        }
        return 0;
    }
    

    SPFA代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdlib>
    #include<sstream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<deque>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<map>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define MM(x) memset(x,0,sizeof(x))
    #define MMINF(x) memset(x,INF,sizeof(x))
    typedef long long LL;
    const double PI=acos(-1.0);
    const int M=20010,N=10010;
    struct info
    {
        int to;
        int pre;
        int dx;
    }E[M];
    int head[N],cnt,deg[N];
    int d[N];
    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=0;
        MM(d);
        MM(deg);
    }
    void add(int s,int t,int d)
    {
        E[cnt].to=t;
        E[cnt].dx=d;
        E[cnt].pre=head[s];
        head[s]=cnt++;
    }
    void spfa(int s)
    {
        typedef pair<int,int> pii;
        priority_queue<pii>Q;
        Q.push(pii(d[s],s));
        while (!Q.empty())
        {
            int now=Q.top().second;
            Q.pop();
            for (int i=head[now]; i!=-1; i=E[i].pre)
            {
                int v=E[i].to;
                if(d[v]>d[now]+E[i].dx)
                {
                    d[v]=d[now]+E[i].dx;
                    Q.push(pii(d[v],v));
                }
            }
        }
    }
    int main(void)
    {
        int n,m,i,j,a,b,c;
        while (~scanf("%d%d",&n,&m))
        {
            init();
            for (i=0; i<m; i++)
            {
                scanf("%d%d",&a,&b);
                add(b,a,-1);
                deg[a]++;
            }
            queue<int>Q;
            int flag=0;
            for (i=1; i<=n; i++)
            {
                if(!deg[i])
                {
                    Q.push(i);
                    flag++;
                }
            }
            while (!Q.empty())
            {
                spfa(Q.front());
                Q.pop();
            }
            int r=0;
            for (i=1; i<=n; i++)
            {
                if(d[i]==0)
                    flag--;
                r=r-d[i]+888;
            }
    		flag?puts("-1"):printf("%d
    ",r);
        }
        return 0;
    }
  • 相关阅读:
    HTML5
    js实现查找字符串中最多的字符的个数
    get和post的区别
    第十七篇 类的特殊成员
    第十八篇 面向对象修饰符
    MariaDB+Keepalived双主高可用配置MySQL-HA
    linux命令详解——crontab
    Java的内存泄漏
    jvm监控工具jconsole进行远程监控配置
    loadrunner执行场景时报Error -27040: Data Format Extension: Init: Internal error问题解决
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766314.html
Copyright © 2011-2022 走看看