RewardCrawling in process...Crawling failedTime Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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注意数值太大 用邻接表存#include <stdio.h> #include <string.h> #include <stdlib.h> #define inf 99999999 struct node { int u; int v; int next; }p[20010]; int in[10010]; int cost[10010]; int vis[10010]; int dis[20010]; int n,m,i,j,k,t,count; int sum,flag; void topo() { memset(vis,0,sizeof(vis)); count=0; sum=0; flag=1; for(i=1;i<=n;i++) { cost[i] = 888; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(in[j]==0) { in[j]--; count++; sum+=cost[j]; for(k=dis[j];k;k=p[k].next) { in[p[k].v]--; if(cost[p[k].v]<cost[j]+1) { cost[p[k].v] = cost[j]+1; } } } } } if(count==n) printf("%d\n",sum); else printf("-1\n"); } int main() { int a,b; while(~scanf("%d %d",&n,&m)) { memset(in,0,sizeof(in)); memset(dis,0,sizeof(dis)); memset(cost,0,sizeof(cost)); t=1; for(i=0;i<m;i++) { scanf("%d %d",&a,&b); p[t].u=b; p[t].v=a; p[t].next=dis[b]; dis[b]=t; t++; in[a]++; } topo(); } return 0; }