您的位置:首页 > 其它

HDU2647

2016-05-26 16:01 232 查看


Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7031    Accepted Submission(s): 2187


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.

 

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

 

Author

dandelion

 

Source

曾是惊鸿照影来

 

Recommend

yifenfei   |   We have carefully selected several similar problems for you:  3342 1811 2680 2112 2094 

 

Statistic | Submit | Discuss | Note

思路:a b 表示a要比b多,所以会以b作为基础,那么久应该把b作为from(出度),a作为to(入度)。

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>

using namespace std;

const int maxn = 10005;
int val[maxn];
int in[maxn];
vector <int > G[maxn];
int n,m,sum,ans;

void topsort()
{
queue <int > que;
while(!que.empty())que.pop();
for(int i = 1;i <= n;i++)
{
if(!in[i])	//这些人的奖金为888
que.push(i);
}
while(!que.empty())
{
int u = que.front();que.pop();
sum += val[u];
ans++;
for(int i = 0;i < G[u].size();i++)
{
int k = G[u][i];
if(--in[k] == 0)
{
val[k] = val[u] + 1;
que.push(k);
}
}
}
}

int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
for(int i = 0;i < maxn;i++)
{
val[i] = 888;
G[i].clear();
}
memset(in,0,sizeof(in));
sum = 0;
ans = 0;

int from,to;
for(int i = 1;i <= m;i++)
{
scanf("%d%d",&to,&from);
G[from].push_back(to);
in[to]++;
}
topsort();
if(ans != n)
sum = -1;
printf("%d\n",sum);
}
return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 10001;
int val[maxn],in[maxn],head[maxn];
int sum,ans,n,m;

struct Node //链接表存边
{
int to,next;
}edge[maxn<<1];

void topsort()
{
queue <int >que;
for(int i = 1;i <= n;i++)
{
if(in[i] == 0)  //入度为0表示这个人奖金就是888
que.push(i);
}
while(!que.empty())
{
int u = que.front();que.pop();
sum += val[u];
ans++;  //用一个变量记录调用元素的总量,最后与n作比较
for(int i = head[u];i != -1;i = edge[i].next)//与队首元素u有关的都枚举一遍
{
int k = edge[i].to;
if(--in[k] == 0)    //如果入度-1为0,即为u的下一个元素
{
que.push(k);
val[k] = val[u] + 1;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
for(int i=1; i<=n; i++)
val[i]=888;//所有人一开始都为888
sum=0;
ans=0;

int from,to,tot=0;
for(int i = 1;i <= m;i++)
{
scanf("%d%d",&to,&from);    //注意要逆过来,因为后一个from是基础的888,应当作为出度
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
in[to]++;   //入度
}
topsort();
if(ans != n)
sum = -1;
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: