HDU2647 topsort

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

拓扑排序专题

vector 存图 注意vector的使用

算是拓扑排序的模板题目吧

多线程的  此题精妙在L数组 L初始为0

#include<bits/stdc++.h>
using namespace std;
vector<int> mp[20005];
int n,m;
int a,b;
int in[20005];
int L[20005];
int flag;
int re;
struct node
{
    int sum;
};
queue<node>q;
struct node N,now;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            mp[i].clear();
        memset(in,0,sizeof(in));
        memset(L,0,sizeof(L));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            mp[b].push_back(a);
            in[a]++;
        }
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0)
            {
                N.sum=i;
                q.push(N);
            }
        }
        int jishu=n;
        int temp;
           while(!q.empty())
            {
                now=q.front();
                q.pop();
                jishu--;
                temp=L[now.sum];
                //cout<<now.sum<<endl;
                for(unsigned int i=0;i<mp[now.sum].size();i++)
                {
                     if(--in[mp[now.sum][i]]==0)
                      {
                          N.sum=mp[now.sum][i];
                          L[N.sum]=temp+1;
                          q.push(N);
                      }
                }
            }
            re=0;
            if(jishu>0)
                printf("-1\n");
            else
            {
                for(int i=1;i<=n;i++)
                    re+=L[i];
                printf("%d\n",re+888*n);
            }
}
    return 0;
}
时间: 2024-12-29 23:56:00

HDU2647 topsort的相关文章

POJ - 3249 Test for Job (DAG+topsort)

Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for

HDU 1285 确定比赛名次 Topsort

题目大意:称职的裁判员忘记了比赛的名次,但还记得谁战胜了谁,给定M组条件,例如A->B 表示A战胜了B,保证信息之间不相互排斥,求输出比赛的名次. 题目分析:是明显的拓扑排序: 在此再介绍一下topsort,核心思想是通过给定的有向无环图确定每个点的入度,比如map[i][j]即表示i战胜了j,此时j的入度+1.但如果存在K点的入度为零,则表示没有人战胜了它,那么他即使排名第一的,然后删除该节点,再以该节点为起始节点,将K战胜的队伍入度-1,再重复上述环节. 总结一下就是: pre:建图,确定每

HDU2647(拓扑排序+反向建图)

题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,由于尽可能的让老板少付钱,那么a的工资就是b的工资+1,可以确定关系为a>b,根据拓扑排序建边的原则是把"小于"关系看成有向边,那么我们可以建边v->u. #include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <algorithm> #

【拓扑排序topsort】【p1226】神经网络

描述 Description 神经网络就是一张有向图,图中的节点称为神经元,而且两个神经元之间至多有一条边相连,下图是一个神经元的例子: 神经元[编号为1) 图中,X1-X3是信息输入渠道,Y1-Y2是信息输出渠道,C1表示神经元目前的状态,Ui是阈值,可视为神经元的一个内在参数. 神经元按一定的顺序排列,构成整个神经网络.在兰兰的模型之中,神经网络中的神经元分为几层:称为输入层.输出层,和若干个中间层.每层神经元只向下一层的神经元输出信息,只从上一层神经元接受信息.下图是一个简单的三层神经网络

HDU2647

第一道逆拓扑纪念一下... #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string.h> #define maxint 999999999 #define MAXN 10005 #define ll long long using namespace std; int n,m,k,

BC 2015百度之星程序设计大赛 - 热身赛(仅供熟悉比赛环境)(Harry and Magical Computer-topsort)[Template:topsort]

Harry and Magical Computer Accepts: 402 Submissions: 1588 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机.这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的).有一天,这台计算机得到了n个任务要处理,分别标号1到n.这

(tarjan建图+topsort+状态压缩) bzoj 2208

2208: [Jsoi2010]连通数 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1489  Solved: 606[Submit][Status][Discuss] Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. Output 输出一行一个整数,表示该图的连通数. Sample Input 3 010 001 100 Sample

拓扑排序 topsort详解

1.定义 对一个有向无环图G进行拓扑排序,是将G中所有顶点排成一个线性序列,通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列. 举例: h3 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; color: #000000; line-height: 172%; text-align: justify; page-break-inside: avoid; orphans: 0; w

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 264    Accepted Submission(s): 63 Problem Description A topological sort or topological ordering of a directed gr