Codeforces 459E Pashmak and Graph

题目链接:http://codeforces.com/problemset/problem/459/A


题目大意:给一个有向带权图,求一条最长路径,路径上边权值严格递增。


大致做法:

#1:搜索,遇到完全图的时候Tle。

#2:dp,将边权值从小到大排序,这样可以保证从前到后的边是不减的,dp[i]表示将第i条边作为终边所能得到的最大值,很明显dp[i] = f[edge[i].v] + 1; (f[v]表示以之前扫描过的边构图将v点作为终点所能得到的最大值);很明显f[edge[j].v] = max(f[edge[j].v], dp[j])(j是edge[j].v的入边);同时更新的时候要将权值相同的边一起更新。


Tle代码:

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int MAXN = 3 * 100000 + 1;

struct Edge
{
    int v, w, next;
}edge[MAXN];

struct Node
{
    int u, v, w;
}node[MAXN];

int head[MAXN], dp[MAXN], e, n, m, ans;

void init()
{
    e = 0;
    memset(head, -1, sizeof(head));
    memset(dp, -1, sizeof(dp));
}

void add(int u, int v, int w)
{
    edge[e].v = v;
    edge[e].w = w;
    edge[e].next = head[u];
    head[u] = e++;
}

template 
bool read(T &n)
{
    T x = 0, temp = 1;
    char c;
    while (((c = getchar()) < '0' || c > '9') && c != EOF && c != '-');
    if (c == EOF) return false;
    if (c == '-') temp = -1, c = getchar();
    x = c - '0';
    while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
    n = temp * x;
    return true;
}

int dfs(int x, int pre)
{
    if (dp[x] != -1) return dp[x];
    int temp = 1;

    for (int i = head[node[x].v]; i != -1; i = edge[i].next)
    {
        if (edge[i].w > pre)
        {
            temp = max(temp, dfs(i, edge[i].w) + 1);
        }
    }

    return dp[x] = temp;
}

void input()
{
    while (read(n) && read(m))
    {
        init();

        for (int i = 0; i < m; i++)
        {
            read(node[i].u), read(node[i].v), read(node[i].w);
            add(node[i].u, node[i].v, node[i].w);
        }

        for (int i = 0; i < e; i++)
        {
            ans = max(ans, dfs(i, edge[i].w));
        }

        cout << ans << endl;
    }
}

int main()
{
    input();
    return 0;
}

Ac代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int MAXN = 3 * 100000 + 1;
int ans, n, m;
int dp[MAXN], f[MAXN];

struct Edge
{
    int u, v, w;
    bool operator < (Edge x) const
    {
        return w < x.w;
    }
}edge[MAXN];

template 
bool read(T &n)
{
    T x = 0, temp = 1;
    char c;
    while (((c = getchar()) < '0' || c > '9') && c != EOF && c != '-');
    if (c == EOF) return false;
    if (c == '-') temp = -1, c = getchar();
    x = c - '0';
    while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
    n = temp * x;
    return true;
}

void input()
{
    while (read(n) && read(m))
    {
        for (int i = 0; i< m; i++)
        {
            read(edge[i].u), read(edge[i].v), read(edge[i].w);
        }

        sort(edge, edge + m);

        int t = 0;
        ans = 0;

        for (int i = 0; i < m; i++)
        {
            dp[i] = f[edge[i].u] + 1;
            if (edge[i].w != edge[i + 1].w)
            {
                for (int j = t; j <= i; j++)
                {
                    f[edge[j].v] = max(f[edge[j].v], dp[j]);
                }
                t = i + 1;
            }
            ans = max(ans, dp[i]);
        }

        cout << ans << endl;
    }
}

int main()
{
    input();
    return 0;
}


你可能感兴趣的:(ACM->DP)