D - Reward
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit
Status
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
这道题的数据量较大,所以用邻接矩阵会导致内存超限,所以,我又尝试了Map,同样内存超限,然后又用了二维模拟邻接链表,结果数组开的不够,用了结构体,超时,用了带头节点的邻接矩阵,超时,最后用的vector超时。。。崩溃边缘,在网上看别人代码,有用各种的,就没有超时的,崩溃。然后看见!=EOF,我习惯用~,竟然没加。。。加上后,过了。。
简单的拓扑排序,只不过需要建立逆图,然后加的时候注意处理好加的幅度就行了。
#include<stdio.h>
#include<string.h>
#include<string>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f
int main(){
int n,m;
int a,b;
while(~scanf("%d%d",&n,&m)){
vector<int>v[10010];
int in[10010];
memset(in,0,sizeof(in));
for(int i = 0;i<m;i++){
scanf("%d%d",&a,&b);
v[b].push_back(a);
in[a]++;
}
int count = 0;
int ceng = 0;
int s = 0;
while(true){
int number = 0;
int per[10010];
for(int i =1;i<=n;i++){
if(in[i]==0){if(number == 0)ceng++;per[number++] = i;count++;}
}
s+=(888+ceng-1)*number;
if(number == 0&&count < n){puts("-1");break;}
if(count == n){printf("%d\n",s);break;}
for(int i = 0;i<number;i++){
in[per[i]] = -1;
for(int j = 0;j<v[per[i]].size();j++){
in[v[per[i]][j]]--;
}
}
}
}
}
//////////////////////////////////////////////////////////
//邻接表处理
struct node{
int to,next;
}edges[20010];
int main(){
int n,m;
int a,b;
while(~scanf("%d%d",&n,&m)){
int in[10010];
int head[20010];
memset(head,-1,sizeof(head));
//memset(edges,0,sizeof(edges));
memset(in,0,sizeof(in));
for(int i = 0;i<m;i++){
scanf("%d%d",&a,&b);
edges[i].to = a;
edges[i].next = head[b];
head[b] = i;
in[a]++;
}
int count = 0;
int ceng = 0;
int s = 0;
while(true){
int number = 0;
int per[10010];
for(int i =1;i<=n;i++){
if(in[i]==0){if(number == 0)ceng++;per[number++] = i;count++;}
}
s+=(888+ceng-1)*number;
if(number == 0&&count < n){puts("-1");break;}
if(count == n){printf("%d\n",s);break;}
for(int i = 0;i<number;i++){
in[per[i]] = -1;
for(int j = head[per[i]];j!=-1;j = edges[j].next){
in[edges[j].to]--;
}
}
}
}
}
“`
//结构体模拟处理
struct node{
int st,to,s;
}edges[20010];
int main(){
int n,m;
int a,b;
while(~scanf("%d%d",&n,&m)){
int in[10010];
memset(edges,0,sizeof(edges));
memset(in,0,sizeof(in));
for(int i = 0;i<m;i++){
scanf("%d %d",&a,&b);
edges[i].to = a;
edges[i].st = b;
in[a]++;
}
int count = 0;
int ceng = 0;
int s = 0;
while(true){
int number = 0;
int per[10010];
for(int i =1;i<=n;i++){
if(in[i]==0){if(number == 0)ceng++;per[number++] = i;count++;}
}
s+=(888+ceng-1)*number;
if(number == 0&&count < n){puts("-1");break;}
if(count == n){printf("%d\n",s);break;}
for(int i = 0;i<number;i++){
in[per[i]] = -1;
for(int j = 0;j<m;j++){
if(edges[j].st == per[i])
in[edges[j].to]--;
}
}
}
}
}