hdoj2647Reward《《拓(tuo)扑》》
2016-03-22 13:04
162 查看
Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6708 Accepted Submission(s): 2076
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
Author
dandelion
Source
曾是惊鸿照影来
越想越难
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
这星期要ac
额,先放两个wrong 代码,,希望给你们启发,,,
第三个ac...没用vector
第四个用STL——vector的正在努力中。。。。。。。。。。
代码1://用的矩阵。。。Memory Limit Exceeded
正确代码:发现上一个代码最后n加上的是m
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/quiet.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/quiet.gif)
。。。先已更正。。。。。。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6708 Accepted Submission(s): 2076
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
Author
dandelion
Source
曾是惊鸿照影来
越想越难
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cute.gif)
这星期要ac
额,先放两个wrong 代码,,希望给你们启发,,,
第三个ac...没用vector
第四个用STL——vector的正在努力中。。。。。。。。。。
代码1://用的矩阵。。。Memory Limit Exceeded
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; int n,m,jisuan,a,b,s; int rudu[10100]; bool map[10100][10100]; bool fafe[10100]; void topo() { queue<int> que; for (int i=1;i<=n;i++) if (rudu[i]==0) { jisuan++; que.push(i); } while (!que.empty()) { int now=que.front(); que.pop(); for (int i=1;i<=n;i++) { if (map[i][now]) { rudu[i]--; map[i][now]=false; s++; if (rudu[i]==0) { que.push(i); jisuan++; } } } } } int main() { while (~scanf("%d%d",&n,&m)) { memset(map,false,sizeof(map)); memset(rudu,0,sizeof(rudu)); for (int i=0;i<m;i++) { scanf("%d%d",&a,&b); map[a][b]=true; rudu[a]++; } s=888*n; jisuan=0; topo(); if (jisuan==n) printf("%d\n",s); else printf("-1\n"); } return 0; }代码2://用aaa[ ],bbb[ ] 带替矩阵,,发现wrong了。。。。正在努力中......................
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; int n,m,jisuan,a,b,s; /*struct node{ int ge; int rushu; int shu; bool friend operator <(node xx,node yy) { return xx.rushu>yy.rushu; } };*/ int rudu[10100]; //bool map[10100][10100]; int aaaa[20050],bbbb[20050]; bool fafe[10100]; void topo() { queue<int> que; for (int i=1;i<=n;i++) if (rudu[i]==0) { jisuan++; que.push(i); } while (!que.empty()) { int now=que.front(); que.pop(); for (int i=0;i<m;i++) { if (bbbb[i]==now) { bbbb[i]=0; int kk=aaaa[i]; rudu[kk]--; s++; if (rudu[kk]==0) { que.push(kk); jisuan++; } } } /*for (int i=1;i<=n;i++) { if (map[i][now]) { rudu[i]--; map[i][now]=0; // map[i][now]=false; s++; if (rudu[i]==0) { que.push(i); jisuan++; } } }*/ } } int main() { while (~scanf("%d%d",&n,&m)) { //memset(map,0,sizeof(map)); memset(rudu,0,sizeof(rudu)); for (int i=0;i<m;i++) { scanf("%d%d",&a,&b); aaaa[i]=a; bbbb[i]=b; //map[a][b]=1; rudu[a]++; } s=888*n; jisuan=0; topo(); if (jisuan==n) printf("%d\n",s); else printf("-1\n"); } return 0; }
正确代码:发现上一个代码最后n加上的是m
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/quiet.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/quiet.gif)
。。。先已更正。。。。。。
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; int n,m,jisuan,a,b,s; int rudu[10100]; int aaaa[20050],bbbb[20050]; bool fafe[10100]; void topo() { queue<int> que; memset(fafe,true,sizeof(fafe)); int ss=0; while (1) { int pp=0; for (int i=1;i<=n;i++) if (rudu[i]==0&&fafe[i]) { pp=1; jisuan++; que.push(i); } if (pp==0) break; while (!que.empty()) { int now=que.front(); que.pop();s+=ss; fafe[now]=false; for (int i=0;i<m;i++) { if (bbbb[i]==now) { bbbb[i]=0; int kk=aaaa[i]; rudu[kk]--; } } } ss++; } } int main() { while (~scanf("%d%d",&n,&m)) { memset(rudu,0,sizeof(rudu)); memset(aaaa,0,sizeof(aaaa)); memset(bbbb,0,sizeof(bbbb)); for (int i=0;i<m;i++) { scanf("%d%d",&a,&b); aaaa[i]=a; bbbb[i]=b; rudu[a]++; } s=888*n; jisuan=0; topo(); if (jisuan==n) printf("%d\n",s); else printf("-1\n"); } return 0; }
相关文章推荐
- iOS 原生二维码扫描crush([AVCaptureMetadataOutput setMetadataObjectTypes:] - unsupported type found. Use -)
- 网易笔试编程题-java版
- VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION
- 黑马程序员--多线程
- 训练深度神经网络的时候需要注意的一些小技巧
- Python多维数组和矩阵
- Activity的生命周期
- QEMU调试Linux系统的USB协议栈
- MS SQL查询所有表行数,获取所有数据库名,表名,字段名
- 互联网架构设计之Redis篇-【Redis Windows版本安装过程】
- 【HDU 1846】Brave Game(巴什博弈)
- 优化网页速度的7种方法
- 蓝桥杯 算法训练 区间k大数查询
- 链表的简单创建——C程序设计
- 第三方 融云集成下
- Http协议和Https协议的区别
- 动态修改 drawableTop
- [mark] 使用Sublime Text 2时如何将Tab配置为4个空格
- 关于编程的那点事
- 互联网架构设计之Redis篇-【Redis的概述】