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 <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>

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 <class T>
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 <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>

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 <class T>
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值