CF-721C DAG图拓扑排序+费用DP
比赛的时候写了个记忆化搜索,超时了。
后来学习了一下,这种题目应该用拓扑排序+DP来做。
dp[][]保存走到[第i个节点][走过j个点]时所用的最短时间。
pre[][]用前驱节点求路径
然后遍历一下dp[n][],求满足t范围的最大下标。
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <stack>
#define N 5050
#define INF 1000000200
//#define LL long long int
using namespace std;
int n,m,sum,cnt,flag,t;
int deg[N];
vector<int> g[N],f[N];//保存后继节点
int dp[N][N],pre[N][N];//保存走到第i个点,走过j个点时所用时间
struct cmp
{
bool operator()(int a,int b)
{
return a>b;
}
};
void topoSort()
{
priority_queue<int,vector<int>,cmp> q;
for(int i=;i<=n;i++)
if(deg[i]==)
q.push(i),deg[i]--;
while(!q.empty())
{
//if(q.size()>1) 可用于判断是否充分排序。
//如果有多个入度为0的同时入队,说明他们之间没有明确的排序条件。
int u=q.top();
q.pop();
sum--;//可用于判断是否有冲突,如果有冲突,就会导致两者或者已上的节点入度无法降为0
for(int i=;i<g[u].size();i++)
{
int e=g[u][i];
deg[e]--;
for(int j=;j<=n;j++)
{
//cout<<dp[u][i]<<' '<<e<<endl;
if(dp[e][j]>dp[u][j-]+f[u][i])
dp[e][j]=dp[u][j-]+f[u][i],pre[e][j]=u;
}
}
for(int i=;i<=n;i++)
if(deg[i]==)
q.push(i),deg[i]--;
}
}
void ini()
{
for(int i=;i<=n;i++)
deg[i]=,flag=,g[i].clear(),f[i].clear();
cnt=,sum=n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dp[i][j]=t+,pre[i][j]=-;
dp[][]=;
}
void add(int u,int v,int fe)
{
deg[v]++;
g[u].push_back(v);
f[u].push_back(fe);
}
int main() {
//cin.sync_with_stdio(false);
scanf("%d%d%d",&n,&m,&t);
ini();
for(int i=;i<m;i++)
{
int u,v,fe;
scanf("%d%d%d",&u,&v,&fe);
add(u,v,fe);
}
//cout<<dp[1][2]<<endl;
topoSort();
int Maxp=-;
for(int i=;i<=n;i++)
{
if(dp[n][i]<=t)
Maxp=i;//求最大合法经过点数
}
stack<int> ss;
int pos=n,lef=Maxp;
cout<<Maxp<<endl;
while(pos!=-)
{
ss.push(pos);
pos=pre[pos][lef],lef--;
}
//ss.push(1);
while(ss.size()>)
cout<<ss.top()<<' ',ss.pop();
cout<<ss.top()<<endl;
ss.pop(); return ;
}
CF-721C DAG图拓扑排序+费用DP的更多相关文章
- BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP
BZOJ_1916_[Usaco2010 Open]冲浪_分层图+拓扑排序+DP Description 受到秘鲁的马丘比丘的新式水上乐园的启发,Farmer John决定也为奶牛们建 一个水上乐园. ...
- DAG及拓扑排序
1.有向无环图和拓扑排序 有向无环图(Directed Acyclic Graph,简称DAG):拓扑排序指的对DAG一个有序的线性排列.即每次选出一个没有入度的节点,然后输出该点并将节点和其相关连的 ...
- HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)
逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- Bzoj 1565: [NOI2009]植物大战僵尸 最大权闭合图,拓扑排序
题目: http://cojs.tk/cogs/problem/problem.php?pid=410 410. [NOI2009] 植物大战僵尸 ★★★ 输入文件:pvz.in 输出文件:p ...
- BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序
BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序 Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息 ...
- 大数据工作流任务调度--有向无环图(DAG)之拓扑排序
点击上方蓝字关注DolphinScheduler(海豚调度) |作者:代立冬 |编辑:闫利帅 回顾基础知识: 图的遍历 图的遍历是指从图中的某一个顶点出发,按照某种搜索方法沿着图中的边对图中的所有顶点 ...
- 拓扑排序+数学+DP【p1685】游览
Description 顺利通过了黄药师的考验,下面就可以尽情游览桃花岛了! 你要从桃花岛的西头开始一直玩到东头,然后在东头的码头离开.可是当你游玩了一次后,发现桃花岛的景色实在是非常的美丽!!!于是 ...
- HDU 5811 Colosseo(拓扑排序+单调DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...
随机推荐
- Windows Installation
If you're running on Windows it is good to run Jenkins as a service so it starts up automatically wi ...
- CentOS7 搭建Docker
搭建环境 Docker支持一下的CentOS版本 CentOS 6.5 (64-bit)或者更高版本 CentOS 7 (64-bit) 搭建条件 Docker运行在CentOS 7上,要求系统64位 ...
- Linux驱动开发调试 -- 打开dev_dbg()【转】
本文转载自:https://blog.csdn.net/kunkliu/article/details/78048618 转载地址:http://blog.chinaunix.net/uid-2284 ...
- FileAttributes Enum
https://docs.microsoft.com/en-us/dotnet/api/system.io.fileattributes?view=netframework-4.7.2 读取FileA ...
- 题解—— 洛谷 p1269 信号放大器(贪心)
深刻的教训,不要写错读入 #include <cstdio> #include <algorithm> using namespace std; ; ; ,u[MAXM],v[ ...
- [mybatis错误] - sql出错 org.apache.ibatis.ognl.ParseException: Encountered "!" at line 1, column 15. Was expecting one of:
完整异常:Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'developerTy ...
- Derek解读Bytom源码-创世区块
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- mysql-5.6.41-winx64安装
安装包 链接:https://pan.baidu.com/s/11-Ts3SrfJViQEtdtI_ik9w 提取码:cxt3 1.解压 将下载好的mysql-5.6.41-winx64.zip的安装 ...
- Linux 中 MySQL常用命令
一. 数据库登录mysql -uroot -p二..退出数据库quit 和 exit或ctrl + d三.数据库操作1. 查看所有数据库 show databases;2. 查看当前使用的数据库sel ...
- RTTI(运行时类型识别),typeid,dynamic_cast
dynamic_cast注意: 1.只能应用于指针和引用的转换: 2.要转换的类型中必须包含虚函数: 3.转换成功则返回地址,如果失败则返回NULL: 参见项目:RTTI