Reward

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

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
 

wa了很多次,一直没找出错在哪,想了很久,发现了错误。

{6 6 2 6 3 6 4 2 4 3 4 1 4 5}这组数据的结果就不对。

如图:

最开始的程序算出的结果为5331,但正确答案为5332。找到了问题。需要每次对当前入度为0的点的后继更新值,而不是当后继度数为零了才更新。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<stack>
using namespace std; int n,m;
vector<int> v[]; int degree[];
int mon[]; void topusort()
{
stack<int>s;
for(int i=; i<=n; i++)
if(degree[i]==)
{
mon[i]=;
s.push(i);
}
long long tot=,cnt=;
while(!s.empty())
{
int h=s.top();
degree[h]--;
s.pop();
cnt++;
tot+=mon[h];
for(int i=; i<v[h].size(); i++)
{
degree[v[h][i]]--;
mon[v[h][i]]=max(mon[v[h][i]],mon[h]+); //做出的修改
if(degree[v[h][i]]==)
{
//mon[v[h][i]]=mon[h]+1; //之前的问题就出在这
s.push(v[h][i]);
}
}
}
if(cnt==n)
printf("%I64d\n",tot+*n);
else
printf("-1\n");
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(degree,,sizeof(degree));
memset(mon,,sizeof(mon));
for(int i=; i<=n; i++)
v[i].clear();
for(int i=; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[b].push_back(a);
degree[a]++;
}
//cout<<degree[1]<<endl;
topusort();
}
return ;
} /*
7 8
2 1
3 1
4 1
5 2
5 3
6 4
7 5
7 6
*/

最新文章

  1. C#——传值参数(1)
  2. CSS3——3D旋转图(跑马灯效果图)
  3. gulp同步执行任务
  4. 配置OpenCV产生flann\logger.h(66): error C4996: &#39;fopen&#39;: This function or variable may be unsafe问题[zz]
  5. PHP 数据库抽象层pdo
  6. 【Android开发坑系列】之事件
  7. php中常用魔术方法的举例
  8. UI进阶 数据加密
  9. Qt中绘图坐标QPainter,Viewport与Window的关系
  10. Selenium之利用Excel实现参数化
  11. Netty 5.0源码分析-ByteBuf
  12. Sql去重
  13. [BZOJ1112] [POI2008] 砖块Klo (treap)
  14. Maven之自定义archetype生成项目骨架
  15. C++用LuaIntf调用Lua代码示例
  16. jquery获取一组文本框的值
  17. [COCI2015]COCI
  18. Unity 3D学习心得,程序员开发心得分享!
  19. 基于centos7的真实机环境下安装 vmware workstastion
  20. 误删centos的ps命令,恢复

热门文章

  1. Angularjs中添加HighCharts
  2. 今天開始慢下脚步,開始ios技术知识的查漏补缺。
  3. zoj 1880 - Tug of War
  4. Android studio一些设置项
  5. 关于PowerDesigner导出数据库表到word文档
  6. 布局技巧4:使用ViewStub
  7. Bing Maps进阶系列四:路由功能服务(RouteService)
  8. bzoj2982 combination——卢卡斯定理
  9. E20170804-mk
  10. Hotel booking(spfa+floyd)