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<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
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<int>Q;
int main(void)
{
int n,m,i,j,a,b,c;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
add(b,a);
deg[a]++;
}
while (!Q.empty())
Q.pop();
int C=0;
for (i=1; i<=n; i++)
{
if(!deg[i])
{
Q.push(i);
C++;
}
}
while (!Q.empty())
{
int now=Q.front();
Q.pop();
dfs(now,0);
}
for (i=1; i<=n; i++)
{
if(!level[i])
C--;
}
if(C)
puts("-1");
else
{
int r=0;
for (i=1; i<=n; i++)
r+=888+level[i];
printf("%d\n",r);
}
}
return 0;
}

SPFA代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
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<int,int> pii;
priority_queue<pii>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<m; i++)
{
scanf("%d%d",&a,&b);
add(b,a,-1);
deg[a]++;
}
queue<int>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;
}

HDU——2647Reward(DFS或差分约束)的更多相关文章

  1. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

  2. hdu 1531 king(差分约束)

    King Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. hdu 1534 Schedule Problem (差分约束)

    Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  5. HDU.1529.Cashier Employment(差分约束 最长路SPFA)

    题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...

  6. HDU 1384 Intervals(差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. hdu 1531(差分约束)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...

  8. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

  9. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

随机推荐

  1. Slacklining 2017/2/6

    原文 Get ready for a different kind of challenge What happens when mountain climbers take a day off?Ap ...

  2. ubuntu 16.0安装 hadoop2.8.3

    环境:ubuntu 16.0 需要软件:jdk ssh https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 2.8.3 安装 jdk并 ...

  3. SAP云平台,Netweaver,Kubernetes和C4C的用户和角色关系

    SAP云平台 Netweaver 同SAP云平台一样,在事务码PFCG里维护角色: 然后在事务码SU01里将多个角色分配给用户: Kubernetes Kubernetes在1.3版本中发布了alph ...

  4. HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场

    题意: 给出一个仙人掌图,然后求他的前K小生成树. 思路: 先给出官方题解 由于图是一个仙人掌,所以显然对于图上的每一个环都需要从环上取出一条边删掉.所以问题就变为有 M 个集合,每个集合里面都有一堆 ...

  5. JavaScript异步仿同步(控制流)的实现

    在前端开发中尤其是在nodejs开发中经常会遇到这样的场景(以ajax为例):有3个(或者更多个)Ajax请求,并且第2个请求依赖于第1个,第3个请求依赖于第2个,那我们可能就会在发第一个Ajax后回 ...

  6. 什么是Java内存模型中的happens-before

    Java内存模型JMM Java内存模型(即Java Memory Model , 简称JMM),本身是一种抽象的概念,并不真实存在,它描述的是一组规则或规范,通过这组规范定义了程序个各个变量(包括实 ...

  7. js正则函数match、exec、test、search、replace、split使用集合

    match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...

  8. Bootstrap历练实例:基本按钮组

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. iptables 过滤字符串

    iptables 过滤字符串 1. 开启iptables iptables -P OUTPUT ACCEPT       ###允许输出链 service iptables save          ...

  10. c++中的结构体:声明 定义 初始化

    什么是结构体? 之前的学习中我们知道了数组是一个容器,而且是存放固定大小数据的容器,而且存放的元素的数据类型必须要一致. 比如数据库中有这样的一条记录学号 性别 年龄 成绩 地址应该怎样存放 结构体: ...