• CF721C Journey


    CF721C Journey

    洛谷传送门

    题目描述

    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}u**i,v**i,t**i ( 1<=u_{i},v_{i}<=n,u_{i}≠v_{i},1<=t_{i}<=10^{9}1<=u**i,v**i<=n,u**i�=v**i,1<=t**i<=109 ), meaning that there is a road starting from showplace u_{i}u**i and leading to showplace v_{i}v**i , and Irina spends t_{i}t**i 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的情况下最多经过多少点,并输出一个方案。


    题解:

    思路:DAG上DP。为了保证无后效性,采用先拓扑排序,再在拓扑序上进行拓展DP的方式。

    为什么拓扑排序就能保证DP的无后效性呢?

    因为拓扑序的性质是,对于DAG上的任意一个点,它的父亲们在拓扑序中出现的位置都在它之前。这也就保证了,处理出拓扑序后再DP,能使得每个状态阶段都是正确的答案(因为转移而来的节点都被处理完了)。

    所以就是先topsort再DP。DP过程中需要记录一个前缀序列。

    代码:

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<iostream>
    using namespace std;
    const int maxn=5002;
    int n,m,dist,maxx;
    int tot,to[maxn],nxt[maxn],val[maxn],head[maxn];
    int rudu[maxn];
    queue<int> q;
    vector<int> vec;
    stack<int> st;
    int dp[maxn][maxn];
    //dp[i][j]表示从1-i,经过j个点的最短距离
    int pre[maxn][maxn],ans[maxn],cnt;
    void add(int x,int y,int z)
    {
    	to[++tot]=y;
    	nxt[tot]=head[x];
    	val[tot]=z;
    	head[x]=tot;
    }
    void topsort()
    {
    	for(int i=1;i<=n;i++)
    		if(!rudu[i])
    			q.push(i);
    	while(!q.empty())
    	{
    		int x=q.front();
    		q.pop();
    		vec.push_back(x);
    		for(int i=head[x];i;i=nxt[i])
    		{
    			int y=to[i];
    			rudu[y]--;
    			if(!rudu[y])
    				q.push(y);
    		}
    	}
    }
    int main()
    {
    	scanf("%d%d%d",&n,&m,&dist);
    	for(int i=1;i<=m;i++)
    	{
    		int x,y,z;
    		scanf("%d%d%d",&x,&y,&z);
    		add(x,y,z);
    		rudu[y]++;
    	}
    	topsort();
    	memset(dp,127,sizeof(dp));
    	dp[1][1]=0;
    	for(int k=0;k<vec.size();k++)
    	{
    		int x=vec[k];
    		for(int i=head[x];i;i=nxt[i])
    			for(int j=2;j<=n;j++)
    			{
    				int y=to[i];
    				if(dp[x][j-1]+val[i]<dp[y][j])
    				{
    					dp[y][j]=dp[x][j-1]+val[i];
    					pre[y][j]=x;
    				}
    			}
    	}
    	for(int i=n;i>=1;i--)
    		if(dp[n][i]<=dist)
    		{
    			maxx=i;
    			printf("%d
    ",i);
    			break;
    		}
    	int pos=n,dep=maxx;
    	while(pos)
    	{
    		st.push(pos);
    		pos=pre[pos][dep];
    		dep--;
    	}
    	while(!st.empty())
    	{
    		cout<<st.top()<<' ';
    		st.pop();
    	}
    	cout<<endl;
    	return 0;
    }
    
  • 相关阅读:
    聊聊Mysql索引和redis跳表 ---redis的有序集合zset数据结构底层采用了跳表原理 时间复杂度O(logn)(阿里)
    各种排序算法的时间复杂度和空间复杂度(阿里)
    HashMap默认加载因子为什么选择0.75?(阿里)
    深入分析Synchronized原理(阿里面试题)
    Gflags 简明使用
    析构函数 (C++)
    C++11学习
    Visual Studio Code 构建C/C++开发环境
    C++经典排序算法总结
    C++迭代器失效的几种情况总结
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13862537.html
Copyright © 2020-2023  润新知