CodeForces 721C Journey
$dp$,拓扑排序。
记$dp[i][j]$表示走到节点$i$,走过了$j$个点的最小时间,然后就可以递推了。要注意的是节点$1$的入度一开始不一定等于$0$。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-6; void File() {freopen("D:\\in.txt","r",stdin);freopen("D:\\out.txt","w",stdout); } template <class T> inline void read(T &x) {char c = getchar();x = 0;while(!isdigit(c)) c = getchar();while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } }const int INF=0x7fffffff; const int maxn=5010; int dp[maxn][maxn]; struct Edge {int v,nx; int w; }e[maxn]; int h[maxn],sz,r[maxn]; int n,m,T; queue<int>Q; int pre[maxn][maxn];void add(int u,int v,LL w) {e[sz].v=v; e[sz].w=w;e[sz].nx=h[u]; h[u]=sz++; }int main() {scanf("%d%d%d",&n,&m,&T);for(int i=0;i<=n;i++){h[i]=-1;for(int j=0;j<=n;j++){dp[i][j]=INF;pre[i][j]=-1;}}for(int i=1;i<=m;i++){int u,v; LL w; scanf("%d%d%d",&u,&v,&w);add(u,v,w); r[v]++;}dp[1][1]=0;for(int i=1;i<=n;i++) if(r[i]==0) Q.push(i);bool flag=0;while(!Q.empty()){int top=Q.front(); Q.pop();if(top==1) flag=1;if(flag==0){for(int i=h[top];i!=-1;i=e[i].nx){int to=e[i].v;r[to]--;if(r[to]==0) Q.push(to);}continue;}for(int i=h[top];i!=-1;i=e[i].nx){int to=e[i].v;for(int j=1;j<=n;j++){if(dp[top][j-1]==INF) continue;if(dp[top][j-1]+e[i].w>T) continue;if(dp[top][j-1]+e[i].w>=dp[to][j]) continue;pre[to][j]=(top-1)*n+j-1-1;dp[to][j]=dp[top][j-1]+e[i].w;}r[to]--;if(r[to]==0) Q.push(to);}}int sum;for(int i=1;i<=n;i++) if(dp[n][i]<=T) sum=i;cout<<sum<<endl;int nowx=n,nowy=sum; stack<int>S;while(1){S.push(nowx);int tx,ty;tx=pre[nowx][nowy]/n; tx++;ty=pre[nowx][nowy]%n; ty++;if(pre[nowx][nowy]==-1) break;nowx=tx; nowy=ty;}while(!S.empty()){cout<<S.top()<<" "; S.pop();}return 0; }
转载于:https://www.cnblogs.com/zufezzt/p/5925437.html
CodeForces 721C Journey相关推荐
- codeforces 839C Journey
题目链接 http://codeforces.com/problemset/problem/839/C 题意:两个人骑马旅行城市,n个城市被n-1条边相连,相互间可以互相访问.天空大雾,马从1号城市走 ...
- CodeForces - 1321B Journey Planning(思维)
题目链接:点击查看 题目大意:给出一个长度为 n 的数列,规定本题中的上升子序列必须满足两个条件: a[ j ] < a[ i ] a[ i ] - a[ j ] = i - j 问累加和最大的 ...
- DAG最长路问题 hdu-1224
用DFS+记忆化写了一下,拓扑排序+DP的我还没弄明白.据说Codeforces 721C就是这类题目,因为有费用限制,DFS不太好写,有时间把DP法想明白来. #include <iostre ...
- 2019暑假起集训实录
欢迎 2019暑假集训实录 7.6 Codeforces #Round572补题,UVA820最大流,UVA12093树形dp,完成POJ1077优先队列bfs(A*),完成HDU1667-The R ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Gym 101630J Journey from Petersburg to Moscow (最短路)
题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...
- 【CodeForces - 789D】Weird journey(思维,图的性质,tricks,有坑)
题干: Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his ...
- Educational Codeforces Round 103 (Rated for Div. 2) D. Journey dp
传送门 题意: 给n+1n+1n+1个点,每两个点之间有一个字符L(表示i+1连向i)L(表示i+1连向i)L(表示i+1连向i)或者R(表示i连向i+1)R(表示i连向i+1)R(表示i连向i+1) ...
- 【Codeforces】 Round #374 (Div. 2)
Position:http://codeforces.com/contest/721 我的情况 开始还是rank1,秒出C.(11:00机房都走光了,我ma到11:05才走,只打了一个小时) 结果.. ...
最新文章
- VS code 的变量设定
- 【转】同步与异步复位以及相关的亚稳态状况与设计可靠性
- 《千只鹤》:面对爱,我们永远还行走在成长的路上 ...
- java程序设计及实践实践代码_杭+新闻:姚争为老师把程序设计讲“活”了,满是代码的枯燥课程被学生“秒杀”...
- 日本字全角字符linux,Linux命令行输入全角字符被转化
- MATLAB求线性代数的参数范围,MATLAB科学计算04(线性代数问题求解一)
- BZOJ1110 : [POI2007]砝码Odw
- 蓝桥杯2013年省赛C/C++大学组 C/C++
- 汇编语言上机考试三星题——根据键盘输入调用子程序显示子程序的入口地址
- Linux网络编程复习笔记
- 简单分享apache封IP的方法
- LINUX下载编译redis
- App的开发成本是多少?
- 华为招聘状态码RSA解密方法
- 计算机类课题研究方法,课题研究方法有哪些
- MapReduce 与 Database 的乌龙战
- 哈尔滨工业大学计算机系统大作业2022春
- 数据结构实验教程-第一套
- 个人简历——英文单词
- 理光M2004文件服务器,理光mp2014打印机怎么连接到电脑