博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU——2647Reward(DFS或差分约束)
阅读量:5156 次
发布时间:2019-06-13

本文共 3496 字,大约阅读时间需要 11 分钟。

 

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7078    Accepted Submission(s): 2205

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
 

 

 

晚上无聊随便看看,然后发现了这题,网上说可以用拓扑排序,画了个草图,发现拓扑并不好用,而且感觉可能排出来会错,然后就自己想了个DFS,感觉这题用DFS还是满靠谱的,有一个坑点就是可能存在部分成环,即有一部分点正常而另一部分是环,因此WA两次(以为DFS写错了检查半天……)DFS和SPFA时间差不多都是40MS左右。对于图的DFS和BFS遍历还是比较好写的。SPFA判负环就还是用的入度数组,就没用访问次数数组了。

DFS代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define MM(x) memset(x,0,sizeof(x))#define MMINF(x) memset(x,INF,sizeof(x))typedef long long LL;const double PI=acos(-1.0);const int M=20010,N=10010;struct info{ int to; int pre;}E[M];int head[N],cnt;int deg[N];int level[N],vis[N];void init(){ memset(head,-1,sizeof(head)); cnt=0; MM(deg); MM(level);}void add(int s,int t){ E[cnt].to=t; E[cnt].pre=head[s]; head[s]=cnt++;}void dfs(int s,int tl){ vis[s]=1; for (int i=head[s]; i!=-1; i=E[i].pre) { int v=E[i].to; if(!vis[v]) { cnt++; level[v]=max(level[v],tl+1); dfs(v,tl+1); } } vis[s]=0;}queue
Q;int main(void){ int n,m,i,j,a,b,c; while (~scanf("%d%d",&n,&m)) { init(); for (i=0; i

 

SPFA代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define MM(x) memset(x,0,sizeof(x))#define MMINF(x) memset(x,INF,sizeof(x))typedef long long LL;const double PI=acos(-1.0);const int M=20010,N=10010;struct info{ int to; int pre; int dx;}E[M];int head[N],cnt,deg[N];int d[N];void init(){ memset(head,-1,sizeof(head)); cnt=0; MM(d); MM(deg);}void add(int s,int t,int d){ E[cnt].to=t; E[cnt].dx=d; E[cnt].pre=head[s]; head[s]=cnt++;}void spfa(int s){ typedef pair
pii; priority_queue
Q; Q.push(pii(d[s],s)); while (!Q.empty()) { int now=Q.top().second; Q.pop(); for (int i=head[now]; i!=-1; i=E[i].pre) { int v=E[i].to; if(d[v]>d[now]+E[i].dx) { d[v]=d[now]+E[i].dx; Q.push(pii(d[v],v)); } } }}int main(void){ int n,m,i,j,a,b,c; while (~scanf("%d%d",&n,&m)) { init(); for (i=0; i
Q; int flag=0; for (i=1; i<=n; i++) { if(!deg[i]) { Q.push(i); flag++; } } while (!Q.empty()) { spfa(Q.front()); Q.pop(); } int r=0; for (i=1; i<=n; i++) { if(d[i]==0) flag--; r=r-d[i]+888; } flag?puts("-1"):printf("%d\n",r); } return 0;}

转载于:https://www.cnblogs.com/Blackops/p/5766314.html

你可能感兴趣的文章
list control控件的一些操作
查看>>
判断字符串在字符串中
查看>>
201421410014蒋佳奇
查看>>
Xcode5和ObjC新特性
查看>>
Centos 7.0 安装Mono 3.4 和 Jexus 5.6
查看>>
CSS属性值currentColor
查看>>
Real-Time Rendering 笔记
查看>>
实验四2
查看>>
多路复用
查看>>
javascript 无限分类
查看>>
spring IOC装配Bean(注解方式)
查看>>
[面试算法题]有序列表删除节点-leetcode学习之旅(4)
查看>>
kubernetes_book
查看>>
侧边栏广告和回到顶部
查看>>
https://blog.csdn.net/u012106306/article/details/80760744
查看>>
海上孤独的帆
查看>>
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
01: socket模块
查看>>
mysql触发器
查看>>
淌淌淌
查看>>