zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    OJ生成器(一)制作Online Judge前的准备和策划
    中国剩余定理
    图片
    扩展GCD的一点心得
    用css控制连续英文字符或数字换行
    CTFHUB HTTP协议之请求方式
    PHP基础05---文件处理
    PHP基础04---文件包含
    PHP基础03---数组
    PHP基础01
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13862537.html
Copyright © 2011-2022 走看看