首页 技术 正文
技术 2022年11月16日
0 收藏 435 点赞 3,996 浏览 3444 个字

Reward

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

Problem DescriptionDandelion’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. InputOne 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. OutputFor 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 Input2 1
1 2
2 2
1 2
2 1 Sample Output1777
-1 Authordandelion 

晚上无聊随便看看,然后发现了这题,网上说可以用拓扑排序,画了个草图,发现拓扑并不好用,而且感觉可能排出来会错,然后就自己想了个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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,907
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,432
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727