Hdu - 2647 - Reward
上题目
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2522 Accepted Submission(s): 745
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.
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
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相关推荐
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- HDU 2647 Reward 拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意: 输入N和M代表N个人和M组数据,M组数据中的A和B代表A的工资要比B的工资高,底薪是(888元), ...
- HDU 2647 拓扑排序
题意:每个人的工资至少888,然后有m个条件,前者比后者要多.求最少工资. 分析: 最开始的开邻接矩阵的肯定超时,如果dfs,会出现由于刚开始不是从入度为0的点出发,后期修改不了.比较麻烦. 正确方式 ...
- 图论复习(各类习题)
可以结合这篇博客进行复习:http://www.cnblogs.com/z360/p/7363034.html 一.强连通分量.缩点 习题: 洛谷--P2746 [USACO5.3]校园网Networ ...
- ACM 图论入门题(附代码解释)
目录 HDU 1869 六度分离 HDU 1874 畅通工程续 (最短路) HDU 3339 In Action (最短路+01背包) HDU 1162 Eddy's picture(prime算法) ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【HDOJ图论题集】【转】
1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...
- 一系列图论问题[转]
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- 2019 HZNU Winter Training Day 14 Comprehensive Training
A - Choosing Capital for Treeland CodeForces - 219D 题意:有一颗单向边的树,要选取一个结点作为首都.要求是这个结点到其它结点,总共需要翻转的路径数量 ...
最新文章
- [Javascript]闭包是可以访问上一层函数作用域里变量的函数,即便上一层函数已经关闭
- linux通信--信号量
- SAP UI5 this.oModel.createBindingContext will trigger odata request
- [BZOJ 1834] [ZJOI2010]network 网络扩容
- 如何导入一个项目 myeclipse
- 先滑窗后时空联合处理MATLAB,时空联合优化重建方法及系统与流程
- paging library java_Android官方分页组件介绍之Paging的使用详解
- Scala tuple
- vue仿微博评论回复_Vue之 3.0升级
- python读取excel(xlrd)
- android代码设置点击涟漪,android – 为自定义CompoundButton添加涟漪效果
- 方舟服务器炸了怎么修复,《方舟:生存进化》诸事不顺!退款BUG修复服务器又炸...
- 时尚回馈:店铺以帮助飓风桑迪赈灾基金
- 项目生命周期和产品生命周期的不同
- PC与IOS outlook客户端配置大全——(163邮箱、QQ邮箱、谷歌gmail邮箱)
- QCustomPlot图例
- 15款优秀移动APP产品原型设计工具
- [机器学习]基于OpenCV实现最简单的数字识别
- Sting与Calander的基本用法
- 系统可打开最大文件数过小,导致CHECK_NRPE: Error - Could not complete SSL handshake