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 }
  • 相关阅读:
    前端之css样式(选择器)
    html 之表单,div标签等
    前端基础之html
    【动态规划】【背包模板】
    【dp专题1】F
    【dp专题1】B
    hdu 1171 Big Event in HDU 01背包
    HDU 2048 数塔 简单DP
    练习赛2(补题)问题 G: 建设电力系统【最小生成树kruskal模板题】
    【图论】【最小生成树】【kruskal+prime】
  • 原文地址:https://www.cnblogs.com/homura/p/4729647.html
Copyright © 2011-2022 走看看