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的更多相关文章
随机推荐
- codeforces#1152C. Neko does Maths(最小公倍数)
题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...
- ExcelPower_Helper插件下载与更新日志
ExcelPower_Helper插件下载.功能简述与演示 ExcelPower_Helper最新版本为:0.4.5,截止到目前为止. 下载地址: 链接:https://pan.baidu.com/s ...
- kettle变量(var变量)
设置变量/set varibale 1.定义变量(子转换): 原始数据 设置获取变量:点击获取字段,自动获取变量名称和字段名称 引用变量: 输出: kettle.properties 文件存储在.ke ...
- mysql-SELECT子句的顺序
- Python中的eval(),exec()以及其相关函数
1. eval函数 函数的作用: 计算指定表达式的值.也就是说它要执行的Python代码只能是单个运算表达式(注意eval不支持任意形式的赋值操作),而不能是复杂的代码逻辑,这一点和lambda表达式 ...
- http验证
read -p "输入要添加的用户名: " USERNAME read -p "输入密码: " PASSWD printf "$USERNAME:$( ...
- uploadify多文件上传实例--C#
下载uploadify文件 http://www.uploadify.com/ HTML(视图) <html lang="zh-cn"> <head> &l ...
- mysql 数据库表迁移复制
1. 表结构相同的表,且在同一数据库(如,table1,table2) insert into table1 select * from table2 # 完全复制 insert into table ...
- FTP文件上传 支持断点续传 并 打印下载进度(二) —— 单线程实现
这个就看代码,哈哈哈哈哈 需要用到的jar包是: <dependency> <groupId>commons-net</groupId> <artifact ...
- vue axios使用方法
首先安装axios: cnpm install axios -save 安装成功后,在main.js页面引用: import axios from 'axios' import Qs from 'qs ...