上题目

Reward

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

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
这一题我终于AC了,这么多天终于A题了,感动啊T_T,这一题之前没有过超时了,这一次重新做了一遍,终于A了。
题意就是拓扑排序,然后求给不同的员工发的工资最少是多少。由于不同的员工之间有可能会有比较,如果A>B,就要A的工资要比B的多。之前第一次的做法是在得到拓扑排序以后从顶向叶子遍历,记录层数,结果超时了。
这一次的做法是在拓扑排序的同时进行记录层数,如果在当前的结点已经有一个层数的话,那么就要判断是当前新的层数大还是原来的层数大,记录更大的那个层数,这样才可以符合题意。最后只要将所有结点的层数加起来(最下层的结点层数为0),就可以得到需要多增加加金额。
用这种方法时,邻接表是用来记录当前点i比哪些点的工资要少,同时开一个数组保存某一点i比多少个点要大(即保存数目),也就是说这里有点像开了一个邻接表和一个逆邻接表,只是我各取了这两个表的一部分。
这一题不知道会不会出现重边,我用map来记录已经记录了边。
还有这一题要注意的地方是要留意我需要的是i还是node[i].to(前者是当前结点在静态链表里面的指针值,后者是 当前结点的编号(名称))。
先把超时的代码贴上

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <queue>
 5 #define MAX 10000+2
 6 using namespace std;
 7
 8 typedef struct node
 9 {
10     int x0;
11     struct node *next;
12 }node;
13
14 node s[MAX];
15
16 int d[MAX],e[MAX];
17
18 queue<int> p;
19
20 bool deal(int n)
21 {
22     int i,count;
23     node *f,*r;
24     while(!p.empty()) p.pop();
25     count=0;
26     for(i=1;i<=n;i++) if(!e[i]) {p.push(i);}
27     while(!p.empty())
28     {
29         i=p.front();
30         count++;
31         p.pop();
32         f=s[i].next;
33         while(f)
34         {
35             e[f->x0]--;
36             if(!e[f->x0])
37             {
38                 p.push(f->x0);
39                 d[f->x0]=d[f->x0]<(d[i]+1) ? (d[i]+1) : d[f->x0];
40             }
41             r=f;
42             f=f->next;
43             free(r);
44         }
45     }
46     if(n==count) return 1;
47     return 0;
48 }
49
50 void jadge(int n)
51 {
52     int i,ans;
53     ans=0;
54     for(i=1;i<=n;i++) ans+=d[i];
55     printf("%d\n",ans+888*n);
56 }
57
58 void insert(int x,int y)
59 {
60     node *f;
61     f=&s[x];
62     while(f->next)
63     {
64         if(f->x0==y) return ;
65     }
66     f->next=(node*)malloc(sizeof(node));
67     f->next->next=NULL;
68     f->next->x0=y;
69     e[y]++;
70 }
71
72 int main()
73 {
74     int n,m,i,x,y;
75     //freopen("data.txt","r",stdin);
76     while(scanf("%d %d",&n,&m)!=EOF)
77     {
78         for(i=1;i<=n;i++) {s[i].x0=i;s[i].next=NULL;}
79         memset(d,0,sizeof(d));
80         memset(e,0,sizeof(e));
81         for(i=0;i<m;i++)
82         {
83             scanf("%d %d",&y,&x);
84             insert(x,y);
85         }
86         if(!deal(n)) printf("-1\n");
87         else jadge(n);
88     }
89     return 0;
90 }

2647

上代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <queue>
 5 #include <map>
 6 #include <vector>
 7 #include <algorithm>
 8 #define MAX (10000+2)
 9 using namespace std;
10
11 int head[MAX],tot,co[MAX],f[MAX];
12
13 typedef struct
14 {
15     int to;
16     int next;
17 }Node;
18
19 Node node[MAX<<1];
20
21 map<pair<int,int>,int> M;
22 queue<int> q;
23
24 int topo(int n)
25 {
26     int i,u,count;
27     while(!q.empty()) q.pop();
28     memset(f,0,sizeof(f));
29     count=0;
30     for(i=1;i<=n;i++)
31     {
32         if(!co[i])
33         {
34             f[i]=0;
35             q.push(i);
36         }
37     }
38     while(!q.empty())
39     {
40         u=q.front();
41         q.pop();
42         count++;
43         for(i=head[u];i!=-1;i=node[i].next)
44         {
45             co[node[i].to ]--;
46             if(co[node[i].to]==0)
47             {
48                q.push(node[i].to);
49                f[node[i].to]=f[u]+1 > f[node[i].to] ? f[u]+1 : f[node[i].to];
50             }
51         }
52     }
53     if(count<n) return 0;
54     return 1;
55 }
56
57 int main()
58 {
59     int n,m,i,sum;
60     pair<int,int> e;
61     //freopen("data.txt","r",stdin);
62     while(scanf("%d %d",&n,&m)!=EOF)
63     {
64         memset(head,-1,sizeof(head));
65         memset(co,0,sizeof(co));
66         memset(node,0,sizeof(node));
67         M.clear();
68         tot=0;
69         for(i=0;i<m;i++)
70         {
71             scanf("%d %d",&e.second,&e.first);    //first ==>right   second ==>left
72             if(M.count(e)<=0)
73             {
74                 M.insert(pair<pair<int,int>,int>(e,1));
75                 node[tot].to=e.second;
76                 node[tot].next=head[e.first];
77                 head[e.first]=tot++;
78                 co[e.second]++;
79             }
80         }
81         if(!topo(n)) printf("-1\n");
82         else
83         {
84             sum=0;
85             for(i=1;i<=n;i++) sum+=f[i];
86             sum+=n*888;
87             printf("%d\n",sum);
88         }
89     }
90     return 0;
91 }

View Code

转载于:https://www.cnblogs.com/sineatos/p/3192087.html

Hdu - 2647 - Reward相关推荐

  1. HDU 2647 Reward (拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...

  2. HDU 2647 Reward 拓扑排序

    http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意: 输入N和M代表N个人和M组数据,M组数据中的A和B代表A的工资要比B的工资高,底薪是(888元), ...

  3. HDU 2647 拓扑排序

    题意:每个人的工资至少888,然后有m个条件,前者比后者要多.求最少工资. 分析: 最开始的开邻接矩阵的肯定超时,如果dfs,会出现由于刚开始不是从入度为0的点出发,后期修改不了.比较麻烦. 正确方式 ...

  4. 图论复习(各类习题)

    可以结合这篇博客进行复习:http://www.cnblogs.com/z360/p/7363034.html 一.强连通分量.缩点 习题: 洛谷--P2746 [USACO5.3]校园网Networ ...

  5. ACM 图论入门题(附代码解释)

    目录 HDU 1869 六度分离 HDU 1874 畅通工程续 (最短路) HDU 3339 In Action (最短路+01背包) HDU 1162 Eddy's picture(prime算法) ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  8. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. 2019 HZNU Winter Training Day 14 Comprehensive Training

    A - Choosing Capital for Treeland CodeForces - 219D 题意:有一颗单向边的树,要选取一个结点作为首都.要求是这个结点到其它结点,总共需要翻转的路径数量 ...

最新文章

  1. [Javascript]闭包是可以访问上一层函数作用域里变量的函数,即便上一层函数已经关闭
  2. linux通信--信号量
  3. SAP UI5 this.oModel.createBindingContext will trigger odata request
  4. [BZOJ 1834] [ZJOI2010]network 网络扩容
  5. 如何导入一个项目 myeclipse
  6. 先滑窗后时空联合处理MATLAB,时空联合优化重建方法及系统与流程
  7. paging library java_Android官方分页组件介绍之Paging的使用详解
  8. Scala tuple
  9. vue仿微博评论回复_Vue之 3.0升级
  10. python读取excel(xlrd)
  11. android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果
  12. 方舟服务器炸了怎么修复,《方舟:生存进化》诸事不顺!退款BUG修复服务器又炸...
  13. 时尚回馈:店铺以帮助飓风桑迪赈灾基金
  14. 项目生命周期和产品生命周期的不同
  15. PC与IOS outlook客户端配置大全——(163邮箱、QQ邮箱、谷歌gmail邮箱)
  16. QCustomPlot图例
  17. 15款优秀移动APP产品原型设计工具
  18. [机器学习]基于OpenCV实现最简单的数字识别
  19. Sting与Calander的基本用法
  20. 系统可打开最大文件数过小,导致CHECK_NRPE: Error - Could not complete SSL handshake

热门文章

  1. 揭开发家致富的2个途径
  2. 英伟达发布dpu,或取代cpu,宣称一个能顶125个
  3. 以太坊PoA共识引擎算法介绍(3)
  4. 深度优先搜索(DFS)
  5. Ubuntu配置教程
  6. 调试工具_Apifox for Mac(接口调试管理工具)
  7. 20年研发管理经验谈(七)
  8. Linux升级ssh、ssl
  9. Windows as a Service(4)——使用Intune管理Windows10更新
  10. 每天一个linux命令(55)--at命令