zoukankan      html  css  js  c++  java
  • HDU 2647 Reward(toposort)

    Reward




    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
     
    建反向图编号
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<vector>
     4 using namespace std;
     5 
     6 const int maxn=10005;
     7 
     8 struct Node
     9 {
    10     int c;
    11     int id;
    12 }node[maxn];
    13 
    14 vector<int>G[maxn];
    15 bool indegree[maxn];
    16 int n,m;
    17 
    18 void init()
    19 {
    20     for(int i=1;i<=n;i++)
    21         G[i].clear();
    22     memset(node,0,sizeof(node));
    23     memset(indegree,0,sizeof(indegree));
    24 }
    25 
    26 bool dfs(int u,int id)
    27 {
    28     node[u].c=-1;
    29     for(int i=0;i<G[u].size();i++)
    30         if(node[G[u][i]].c<0)return false;
    31     else if(!dfs(G[u][i],id+1))return false;
    32         node[u].c=1;
    33         node[u].id=max(node[u].id,id);
    34     return true;
    35 }
    36 
    37 bool toposort()
    38 {
    39     for(int i=1;i<=n;i++)
    40         if(!node[i].c&&!indegree[i])
    41         {
    42             if(!dfs(i,0))
    43             return false;
    44         }
    45     for(int i=1;i<=n;i++)
    46         if(!node[i].c)
    47         return false;
    48     return true;
    49 }
    50 
    51 int main()
    52 {
    53     int a,b;
    54     while(scanf("%d%d",&n,&m)!=EOF)
    55     {
    56         init();
    57         for(int i=0;i<m;i++)
    58         {
    59              scanf("%d%d",&a,&b);
    60              G[b].push_back(a);
    61              indegree[a]=true;
    62         }
    63         bool ans=toposort();
    64         if(ans)
    65         {
    66             int temp=0;
    67             for(int i=1;i<=n;i++)
    68                 temp+=node[i].id;
    69             printf("%d
    ",888*n+temp);
    70         }
    71         else
    72             printf("-1
    ");
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    C语言 递归 汉诺塔问题 最大公约数问题
    程序的健壮性及代码风格
    C程序练习
    专题——条件控制循环 猜数游戏 随机种子
    C语言 分支与循环 递推思想 穷举 流程的转移控制
    C指针 指针和数组 二维数组的指针 指针应用
    C语言实现的排序
    数组查找算法的C语言 实现-----线性查找和二分查找
    图片转成base64 跨域等安全限制及解决方案
    移动开发那些坑之——safari mobile click事件的冒泡bug
  • 原文地址:https://www.cnblogs.com/homura/p/4729647.html
Copyright © 2011-2022 走看看