C. Journey(DAG上dp详解)

https://codeforces.com/problemset/problem/721/C

题目描述

Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are nn showplaces in the city, numbered from 11 to nn , and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.

Initially Irina stands at the showplace 11 , and the endpoint of her journey is the showplace nn . Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than TT time units.

Help Irina determine how many showplaces she may visit during her journey from showplace 11 to showplace nn within a time not exceeding TT . It is guaranteed that there is at least one route from showplace 11 to showplace nn such that Irina will spend no more than TT time units passing it.

输入格式

The first line of the input contains three integers n,mn,m and TT ( 2<=n<=5000,1<=m<=5000,1<=T<=10^{9}2<=n<=5000,1<=m<=5000,1<=T<=109 ) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.

The next mm lines describes roads in Berlatov. ii -th of them contains 33 integers u_{i},v_{i},t_{i}ui​,vi​,ti​ ( 1<=u_{i},v_{i}<=n,u_{i}≠v_{i},1<=t_{i}<=10^{9}1<=ui​,vi​<=n,ui​​=vi​,1<=ti​<=109 ), meaning that there is a road starting from showplace u_{i}ui​ and leading to showplace v_{i}vi​ , and Irina spends t_{i}ti​ time units to pass it. It is guaranteed that the roads do not form cyclic routes.

It is guaranteed, that there is at most one road between each pair of showplaces.

输出格式

Print the single integer kk ( 2<=k<=n2<=k<=n ) — the maximum number of showplaces that Irina can visit during her journey from showplace 11 to showplace nn within time not exceeding TT , in the first line.

Print kk distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

If there are multiple answers, print any of them.

题意翻译

给出一个nn个点mm条边的有向无环图。
问从11到nn,在距离不超过kk的情况下最多经过多少点,并输出一个方案。

输入输出样例

输入 #1复制

4 3 13
1 2 5
2 3 7
2 4 8

输出 #1复制

3
1 2 4 

输入 #2复制

6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1

输出 #2复制

4
1 2 4 6 

输入 #3复制

5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2

输出 #3复制

3
1 3 5 

思路:dp[i][j]:到达i这个点经过了j个点的最少花费

转移: dp[v][j]=min(dp[v][j],dp[u][j-1]+w); 注意:这个u是能达到v的点,有很多个类似floyd去更新

为了方便dp的直观性和取消后效性,使用拓扑排序。

例如:  1---->2------>3-------->4------->5

           |                  ^

           |----->6-------|

这里的3应该由能到3的2和6去更新,更新了后再由3去更新4和5.所以当更新3的时候,当前用2更新,下一个改用6去更新,这里就是拓扑排序的过程,方便并且容易理解保证dp的转移。

当然我看了看别的博主也有不用拓扑的。类似换根dp的时候从上往下,先更新dp后然后dfs这个点更新这个点往后的,然后再接着更新再dfs。比如这个博主:https://blog.csdn.net/mitsuha_/article/details/78348347。个人理解是先dp更新到2这个点,然后以2为起点dfs去更新后面的点,更新好了再到6去再dfs更新2后面的点...总之还是拓扑排序之后更容易理解。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e3+100;
typedef int LL;//不习惯 
LL deg[maxn],pre[maxn][maxn],dp[maxn][maxn];
vector< pair<LL,LL> >g[maxn]; 
LL n,m,T;
void topsort()
{
	queue<LL>q;
	for(LL i=1;i<=n;i++)
	{
		if(deg[i]==0) q.push(i);
	}
	while(q.size())
	{
		LL u=q.front();q.pop();
		for(LL i=0;i<g[u].size();i++)
		{
			LL _v=g[u][i].first;LL _w=g[u][i].second;
			for(LL j=2;j<=n;j++)
			{
				if(dp[u][j-1]+_w<dp[_v][j])
				{
					dp[_v][j]=dp[u][j-1]+_w;
					pre[_v][j]=u;
				}
			}
			if(--deg[_v]==0) q.push(_v);
		}
	}
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>n>>m>>T;
  for(LL i=1;i<=m;i++)
  {
  	LL x,y,w;cin>>x>>y>>w;
  	g[x].push_back({y,w});
  	deg[y]++;
  }
  memset(dp,0x3f,sizeof(dp));
  dp[1][1]=0;
  topsort();
  for(LL i=n;i>=2;i--)
  {
  	 if(dp[n][i]<=T)
  	 {
  	 	cout<<i<<endl;
		vector<LL>path;//存对应路径 
		for(LL j=i,t=n;j>=1;j--)
		{
			path.push_back(t);
			t=pre[t][j];	
		}	
		for(LL k=path.size()-1;k>=0;k--)
		{
			cout<<path[k]<<" ";
		}
		cout<<endl;
		return 0;
	 }
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108429918