Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3015 Accepted Submission(s): 907
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
思路:拓扑排序。此题数据量大,使用邻接矩阵超出空间,需要使用链式前向星(静态建邻接表),另外还有一点要注意,要分层排序,每层的花费是一样大的。
其实拓扑排序关键是如何更新节点的入度以及如何在计算机中存储图;
下面是超空间代码:
数据结构:邻接矩阵
map[MAX][MAX];
#include<stdio.h>
#include<string.h>
int degree[],map[][]; int vis[],pre[];
int main()
{
int n,m,a,b,i,j,k,t,p,flag,sum,temp;
while(~scanf("%d%d",&n,&m))
{
memset(degree,,sizeof(degree));
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
t = sum = flag = ;
while(m--)
{
scanf("%d%d",&a,&b);
if(!map[a][b])
{
degree[b]++;
map[a][b] = ;
}
}
for(i = ;i < n;i ++)
{
k = ;
for(j = ;j <= n;j ++)
{
if(degree[j] == && vis[j] == )
pre[k++] = j;
}
sum += (+t)*k;
for(j = ;j < k;j ++)
{
vis[pre[j]] = ;
for(p = ;p <= n;p ++)
{
if(map[pre[j]][p] == && vis[p]== )
{
degree[p]--;
map[pre[j]][p] = ;
}
}
}
t++;
}
for(i = ;i <=n;i ++)
{
if(degree[i])
{
flag = ;
break ;
}
}
if(!flag)
printf("%d\n",sum);
else
printf("-1\n");
}
return ;
}
下面是AC代码:
数据结构:链式前向星
int head[MAX];
typedef struct
{
int to; //记录终点;
int next; //记录下一个节点的位置;
int ... //记录其他一些信息,与题有关;
}EdgeNode;
EdgeNode edge[MAX_m];
cin >> i >> j >> ...;
edge[k].to = j;
edge[k].next = head[i];
edge[k].... = ...;
head[i] = k++;
#include<stdio.h>
#include<string.h>
typedef struct
{
int to;
int next;
}EdgeNode;
EdgeNode Edge[];
int head[],node[];
int cnt,indegree[],vis[];
void init()
{
cnt = ;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(indegree,,sizeof(indegree));
} void add_edge(int n,int m)
{
Edge[cnt].to = n;
Edge[cnt].next = head[m];
head[m] = cnt++;
} int main()
{
int n,m,a,b,i,j,k,t,p,sum;
while(~scanf("%d%d",&n,&m))
{
init();
sum = p = ;
while(m--)
{
scanf("%d%d",&a,&b);
indegree[a]++;
add_edge(a,b);
}
for(i = ;i < n;i ++)
{
t = ;
for(j = ;j <= n;j ++)
{
if(indegree[j] == && vis[j] == )
node[t++] = j;
}
sum += (+p)*t;
p++;
for(j = ;j < t;j ++)
{
vis[node[j]] = ;
for(k = head[node[j]];k != -;k = Edge[k].next)
{
if(!vis[Edge[k].to])
indegree[Edge[k].to]--;
}
}
}
for(i = ;i <= n;i ++)
{
if(indegree[i])
{
sum = -;
break ;
}
}
printf("%d\n",sum);
}
return ;
}