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;
}

最新文章

  1. Spring 4 + Reactor Integration Example--转
  2. 关于float和margin
  3. NeoKylin5.6下安装部署达梦(DM7)数据库
  4. WebView 载入本地的html
  5. connections
  6. Lua-泛型for循环 pairs和ipairs的区别
  7. 《C#编程风格》还记得多少
  8. Android中对JSONArray数组的指定项进行删除,更新。
  9. 下载文档时Safari浏览器下载后出现&quot;.html&quot;问题
  10. jquery animate函数实现
  11. javascript 如何继承父类
  12. QT---系统托盘图标不显示原因
  13. asp.net模板控件示例
  14. C++const使用(06)
  15. esp8266 终于装上固件了!半个月了!开始进军简单粗暴的lua语言!!
  16. Linux下ps -ef和ps aux的区别
  17. 使用nvidia-smi命令查看显卡信息
  18. 15)django-ORM(多对多关系)
  19. 16. pt-mysql-summary
  20. 【struts2】action中使用通配符

热门文章

  1. mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
  2. PostgressSQL-Installation
  3. SQLITE-更新查询
  4. nuxt 头部引入js文件 第一次进入页面不加载js文件的解决方法
  5. python基础一 day9 函数升阶(3)
  6. 多维的vector定义和初始化
  7. 数组初始化 和 vector初始化
  8. CPP-基础:TCHAR
  9. nyoj-47-过河问题|POJ-1700-Crossing River
  10. PAT (Basic Level) Practise (中文)-1033. 旧键盘打字(20)