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 }
  • 相关阅读:
    性格决定命运
    操作系统课程设计之生产者消费者问题
    Linux 操作系统学习之线程
    OpenCV 显示一幅图片
    对图像每个像素点量化
    css选择器
    极简主义,对逻辑操作符||和&&深度运用的理解
    slice的用法与用量
    简单重置移动端默认样式
    移动端视口格式化备注
  • 原文地址:https://www.cnblogs.com/homura/p/4729647.html
Copyright © 2011-2022 走看看