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.
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.
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 ",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 ",r); } return 0; }