codeforces 721C journey

1.暴力dp就可以。

2.不仅1号点没有入度。。。要把整张图拿去拓扑。再初始化的时候仅dp[1][1]=0即可。

3.inf要取好。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define amxn 5050
#define maxv 5050
#define maxe 5050
#define inf 0x3f3f3f3f
using namespace std;
int n,m,t,x,y,z,g[maxv],nume=0,d[maxv],pre[maxv][maxv];
int s1[maxv],t1=0;
int dp[maxv][maxv];
struct edge
{
    int v,nxt;
    int w;
}e[maxe];
queue <int> q;
bool vis[maxv];
void addedge(int u,int v,int w)
{
    e[++nume].v=v;e[nume].w=w;
    e[nume].nxt=g[u];g[u]=nume;
}
void bfs()
{
    dp[1][1]=0;
    for (int i=1;i<=n;i++)
    {
        if (!d[i])
        {
            vis[i]=true;
            q.push(i);
        }
    }
    while (!q.empty())
    {
        int head=q.front();q.pop();
        for (int i=g[head];i;i=e[i].nxt)
        {
            int v=e[i].v;
            if (d[v])
            {
                d[v]--;
                for (int j=2;j<=n;j++)
                {
                    if (dp[v][j]>dp[head][j-1]+e[i].w)
                    {
                        dp[v][j]=dp[head][j-1]+e[i].w;
                        pre[v][j]=head;
                    }
                }
                if (!d[v]) q.push(v);
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        addedge(x,y,z);d[y]++;
    }
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            dp[i][j]=inf;
    bfs();
    int tmp=n+1;
    for (int i=n;i>=1;i--)
    {
        if (dp[n][i]<=t)
        {
            tmp=i;
            break;
        }
    }
    printf("%d\n",tmp);int now=n;
    while (now!=1)
    {
        s1[++t1]=now;
        now=pre[now][tmp];tmp--;
    }
    s1[++t1]=1;
    for (int i=t1;i>=1;i--)
        printf("%d ",s1[i]);
    return 0;
}
时间: 2024-12-24 10:58:16

codeforces 721C journey的相关文章

CodeForces 788B--Weird journey

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland - Uzhlyandia. It is widely known that Uzhlyandia has

[Codeforces 839C] Journey

[题目链接] http://codeforces.com/contest/839/problem/C [算法] 概率DP 时间复杂度 : O(N) [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 int tot , n; int head[MAXN]; bool visited[MAXN]; double f[MAXN]; struct edge { int to , nxt; } e[MAXN <

Codeforces 721C [dp][拓扑排序]

/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG),这题一定可以dp. 2.dp[i][j]代表,到达点i,并且拜访了j个城市的最短时间. wa点: 没有初始化数组中的0.. */ #include<bits/stdc++.h> #define N 5050 using namespace std; int inf=0x3f3f3f3f; int

CodeForces 721B Journey (DP)

题意:给定一个有向图,你从1出发到n,走尽可能多的点,并且使总权值不大于t. 析:在比赛时,竟然看成有向图了,就想了好久,感觉dp,但是不会啊...如果是有向图就好做多了,枚举边,然后打印就好,dp[i][j] 表示, 经过 i 个结点,并且在 j的最小时间. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #inc

Codeforces - 1321B - Journey Planning(思维)

题目链接 题目大意:让你找出一列数,他们中相邻的两个数的下标之差等于数值之差.求这列数的最大值 ??思路很简单,因为所得数列满足相邻的两个数的下标之差等于数值之差.所以只要让每个输入的数减去对应的下标所得到的下标指向的数加上这个数即可,如果两个数的下标之差等于数值之差,那么 它们各自的数减去下标所得到的下标指向的必定是同一个位置.换句话说,这个位置的数等于所有两个数的下标之差等于数值之差的数的和.(这样的位置可以有多个) //https://www.cnblogs.com/shuitiangon

DAG最长路问题 hdu-1224

用DFS+记忆化写了一下,拓扑排序+DP的我还没弄明白.据说Codeforces 721C就是这类题目,因为有费用限制,DFS不太好写,有时间把DP法想明白来. #include <iostream> #include <cstdio> #include <vector> #include <stack> #define LL long long int using namespace std; LL sta[105]; vector<int>

Codeforces 789D Weird journey - 欧拉路 - 图论

Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland — Uzhlyandia. It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the countr

Codeforces Round #374 (Div. 2)-C. Journey DP

C. Journey 题意: 在一个DAG(有向无环图)中,问从1 到 n 点,在时间限制K下,最多能游玩几个地点,把游玩的顺序顺便输出. 思路: 感觉dp,一维不够就加一维,我一开始有想到dp,但是只是一维的去推,推着感觉不正确.这次用dp[i][j],表示到j点已游玩i个地点的最少时间. DAG中一般要想到求拓扑序.即保证dp从左到右. 这题这个人直接两重循环求dp,不知道为啥也是对的 ,确实是有道理,因为dp[ i - 1]这一行的所有情况对于 i 行都是确定的,且i行一定是在i-1行存在

【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered