Week_10 C
拓扑排序
题意:输入n行数据a,b ,表示a的钱数大于b的钱数,最低的人分的的钱数为888,问最少需要多少钱可以分给员工
思路:标准的拓扑排序,不过这题需要逆向拓扑
注意点:1、如何判断途中有换,或者说有的点没有选择到,用个int整型cnt,利用拓扑排序的特点,每个点只查找一次,所以当cnt==n的时候,输出sum。
2、多组输入不要忘了初始化数组之类的
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
#define MAX INT_MAX
#define FOR(i,a,b) for( ll i = a;i <= b;++i)
using namespace std;
int n,m,sum,head,headmoney,a,b,cnt;
int degree[]; //度,我就不多说了
vector<int>vec[]; //存此节点下的子节点
queue<int>que; //存接下来思考的节点
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
// freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(degree,,sizeof(degree));
while(!que.empty()) que.pop();
memset(vec,,sizeof(vec));
sum=;
cnt=; while(m--)
{
cin>>a>>b;
if(a!=b)
{
vec[b].push_back(a);
degree[a]++;
} } for(int i=;i<=n;++i)
{
if(degree[i]==)
{
//cnt++;
//sum+=888;
que.push(i);
que.push();
}
}
while(!que.empty())
{
cnt++; head=que.front();
que.pop();
headmoney=que.front();
que.pop();
sum+=headmoney;
for(int i=;i<vec[head].size();++i)
{
if(--degree[vec[head][i]]==)
{
que.push(vec[head][i]);
que.push(headmoney+);
}
}
}
if(cnt==n)
{
cout<<sum<<endl;
}
else cout<<"-1"<<endl; } }
Week_10 C的更多相关文章
随机推荐
- iOS App上架流程(2016详细版)来源DeveloperLY
一.前言: 作为一名iOSer,把开发出来的App上传到App Store是必要的.下面就来详细讲解一下具体流程步骤. 二.准备: 一个已付费的开发者账号(账号类型分为个人(Individual).公 ...
- spring整合redis使用RedisTemplate的坑Could not get a resource from the pool
一.背景 项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resou ...
- 封装LocalStorage.js
之前使用vue开发的项目频繁使用到localStorage,封装了一下 localStorage.js文件代码如下: let obj = {}; /** * putLocalStorage 把数据放到 ...
- 鼠标拖动DOM
自己收藏,使用angualrjs的directive些的鼠标拖动DOM.... <!DOCTYPE html> <html lang="en"> <h ...
- Python——汇总
一.工具类 (1)pycharm激活.中文破解 (2)pycharm的基本设置 (3)pycharm常用包和插件的安装 二.数据类型 (1)列表.元祖.字典 操作方法 (2)迭代器操作方法 (3)生成 ...
- HDU 4547 CD操作
传送门 没啥好说的.就是一个LCA. 不过就是有从根到子树里任意一个节点只需要一次操作,特判一下LCA是不是等于v.相等的话不用走.否则就是1次操作. 主要是想写一下倍增的板子. 倍增基于二进制.暴力 ...
- Nginx HTTP框架提供的其它变量
L74
- 搭建alpine仓库 提供apk包
搭建alpine私有仓库从官方拉取alpine所有的包 wget -r -np -nH http://nl.alpinelinux.org/alpine/v3.5/main/x86_64/ wget ...
- 【THUSC2017】【LOJ2977】巧克力 斯坦纳树
题目大意 有一个网格(或者你可以认为这是一个图),每个点都有颜色 \(c_i\) 和点权 \(a_i\). 求最小的连通块,满足这个连通块内点的颜色数量 \(\geq k\).在满足点数最少的前提下, ...
- spring-webmvc-DispatcherServlet
Spring Web MVC is the original web framework built on the Servlet API and has been included in the S ...