HDU-2647 Reward(链式前向星+拓扑排序)

摘要:
代表工作数量和需求数量。(n<usingspacestd;stdin);标准输出);标准;“=”<i——)类型定义;常量双pi=acos(-1.0);intn;intvv=0;头[u]=cnt;}lltopo(){inttot=0;i<

Reward

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16602 Accepted Submission(s): 5308

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

解题思路:

反向建图后拓扑排序,就为了复习下链式前向星和拓扑排序

#include <bits/stdc++.h>
using namespace std;
/*    freopen("k.in", "r", stdin);
    freopen("k.out", "w", stdout); */
//clock_t c1 = clock();
//std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 5e5 + 7;
const ll MAXM = 1e6 + 7;
const ll MOD = 998244353;
const double eps = 1e-6;
const double pi = acos(-1.0);
int n, m;
int cnt = -1;
int indegree[MAXN];
int head[MAXN];
int red[MAXN];
struct Edge
{
    int u, v, Next;
    Edge(int uu = 0, int vv = 0, int NN = 0) { u = uu, v = vv, Next = NN; }
} e[MAXN];
void add(int u, int v)
{
    e[++cnt].v = v;
    e[cnt].u = u;
    e[cnt].Next = head[u];
    head[u] = cnt;
}
ll topo()
{
    int tot = 0;
    queue<int> q;
    bool flag1 = false;
    for (int i = 1; i <= n; i++)
        if (!indegree[i])
            q.push(i);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        tot++;
        if (tot > n)
            break;
        for (int i = head[now]; ~i; i = e[i].Next)
        {
            int v = e[i].v;
            indegree[v]--;
            if (!indegree[v])
            {
                q.push(v);
                red[v] = red[now] + 1;
            }
        }
    }
    if (tot != n)
        return -1;
    ll ans = n * 888;
    for (int i = 1; i <= n; i++)
        ans += red[i];
    return ans;
}
int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        cnt = -1;
        memset(red, 0, sizeof(red));
        memset(head, -1, sizeof(head));
        memset(indegree, 0, sizeof(indegree));
        while (m--)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add(v, u);
            indegree[u]++;
        }
        printf("%lld
", topo());
    }
    return 0;
}

免责声明:文章转载自《HDU-2647 Reward(链式前向星+拓扑排序)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇PHP对接口执行效率慢的优化如何基于 PHP-X 快速开发一个 PHP 扩展下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Asp.net2.0 VS 2005下的repeater控件本功能分页实例(共有 条记录 共有几页 当前第 页 首页,上一页,下一页,尾页 DropDownList跳转)

一、预览效果二、前台控件呈现部分 <asp:repeater id="LeaveMessage" runat="server" ><ItemTemplate><table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor=...

FreeMarker语法

向原作者致敬,原文地址http://www.cnblogs.com/linjiqin/p/3388298.html FreeMarker的插值有如下两种类型:1,通用插值${expr};2,数字格式化插值:#{expr}或#{expr;format} ${book.name?if_exists } //用于判断如果存在,就输出这个值 ${book.name...

防止表单重复提交的方法

1、在jsp页面的button添加相关js代码: <input type="button" value="提交" onclick="this.disabled=true;this.form.submit()"> 此方法缺点是用户可能禁用js,此方法就可能失效。 2、session的token机制...

python 之 数据类型初接触

python 之 数据类型初接触 标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Set(集合) Dictionary(字典) Python3 的六个标准数据类型中: 不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);...

svg的stroke属性,神奇的描边

1、stroke 定义一条线,文本或元素轮廓颜色 2、stroke-width 定义一条线,文本或元素轮廓厚度 3、stroke-linecap 描边端点表现形式 <svg> <g fill='none' stroke='black' stroke-width='10'> <path stroke-linecap...

Vue跨层级传递slot的方法

因为业务需要,我们的vue组件分了很多层。但我需要在父组件通过slot指定模板,但不在子组件渲染,而是在孙组件或是再下方的组件去渲染。 比如,我有一个通用的A组件,A组件内引用了B组件,B组件又引用了C组件。C组件的模板内有一部分是需要在A组件中来配置的。 因为中间间隔了1层以上的组件,所以没法通过一般的slot方式解决。于是研究了一下vue的scoped...