zoukankan      html  css  js  c++  java
  • HDU 2647 Reward【拓扑排序】

    题意:工厂发工资,最低工资是888元,然后比他高一层得人的工资是889,依次类推

    因为是从工资低的人推到工资高的人,所以反向建图

    然后就是自己写的时候犯的错误,以为工资是后一个人比前一个人高1元,然后就直接判断是否能形成拓扑序列之后,用n*888+(n-1)*n/2来算了

    这样不对,是后一层的工资比前一层得工资多1元,用一个数组记录下来钱就可以了

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
    14 
    15 typedef long long LL;
    16 const int INF = (1<<30)-1;
    17 const int mod=1000000007;
    18 const int maxn=100005;
    19 
    20 int ans[maxn],nextt[maxn],firstt[maxn],in[maxn],e[maxn],money[maxn];
    21 int n,m,ecnt;
    22 int tot;
    23 
    24 void toposort(){
    25     queue<int> q;
    26     for(int i=1;i<=n;i++)
    27     if(in[i]==0) q.push(i);
    28     
    29     tot=0;
    30     int cnt=0;
    31     while(!q.empty()){
    32         int tmp=q.front();q.pop();
    33         ans[++cnt]=tmp;
    34         tot+=money[tmp];
    35         
    36             for(int k=firstt[tmp];k!=-1;k=nextt[k]){
    37                 in[e[k]]--;
    38                 if(in[e[k]]==0) {
    39                     money[e[k]]=money[tmp]+1;
    40                     q.push(e[k]);
    41                 }
    42             }                    
    43     }
    44     
    45     if(cnt==n) printf("%d
    ",tot);
    46     else printf("-1
    ");
    47 }
    48 
    49 void addedges(int u,int v){
    50     e[++ecnt]=v;
    51     nextt[ecnt]=firstt[u];
    52     firstt[u]=ecnt;        
    53     in[v]++;
    54 }
    55 
    56 
    57 int main(){
    58     while(scanf("%d %d",&n,&m)!=EOF){
    59         
    60         ecnt=0;
    61         memset(in,0,sizeof(in));                
    62         memset(firstt,-1,sizeof(firstt));
    63         for(int i=1;i<=n;i++) money[i]=888;
    64         
    65         while(m--){
    66             int u,v;
    67             cin>>u>>v;
    68             addedges(v,u);            
    69         }
    70         toposort();
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    JavaScript 操作 Cookie
    Java监控文件夹变化
    Cookie与Session的区别
    常用插件
    Plugin 'org.springframework.boot:springbootmavenplugin:' not found
    mysql安装(windows)
    idea 安装社区版
    linux安装tomcat
    将克隆的项目上传到自己的github
    tomcat安装配置
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4456176.html
Copyright © 2011-2022 走看看