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. Javafinal方法

    class Animal{     public final void eat(){         System.out.println("吃");     } } class ...

  2. shiro : java.lang.IllegalArgumentException: Odd number of characters.

    shiro使用的时候: java.lang.IllegalArgumentException: Odd number of characters.    at org.apache.shiro.cod ...

  3. OpenGL 渲染上下文-context

    context理解 OpenGL在渲染的时候需要一个Context,这个Context记录了OpenGL渲染需要的所有信息,可以把它理解成一个大的结构体,它里面记录了当前绘制使用的颜色.是否有光照计算 ...

  4. 高精度A+B

    #include<stdio.h> #include<string.h> int main() { int lenth1,lenth2,n,i,j,k,s; scanf(&qu ...

  5. 如何在Mac OS X中开启或关闭显示隐藏文件命令

    打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...

  6. eclipse 在写XML时 包类名自动提醒的问题

    需要加一个STS插件 配置很简单 参考了  https://blog.csdn.net/HH775313602/article/details/70176531 在 https://spring.io ...

  7. Jarvis OJ-Smashes

    栈溢出之利用-stack-chk-fail from pwn import * old_flag_addr = 0x600d20 new_flag_addr = 0x400d20 #p = proce ...

  8. Python IDE推荐

    八个最佳Python IDE 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Python是一种功能强大.语言简洁的编程语言.本文向大家推荐8个适合pyt ...

  9. h5快速制作工具-企业级. 非个人无水印

    Epub360 Epub是团队引入的专业级H5应用开发工具,能够快速制作出高质量的H5运营交互页面,具有动画控制.交互设定.社交应用和数据应用的特点,其制作过程就类似于制作一个PPT,比较容易上手. ...

  10. 自动化运维工具Ansible

    一.简介 当下有许多的运维自动化工具( 配置管理 ),例如:Ansible.SaltStack.Puppet.Fabric 等. Ansible 一种集成 IT 系统的配置管理.应用部署.执行特定任务 ...