Codeforces 459E Pashmak and Graph(dp+貪心),codeforces459e

來源:互聯網
上載者:User

Codeforces 459E Pashmak and Graph(dp+貪心),codeforces459e

題目連結:Codeforces 459E Pashmak and Graph

題目大意:給定一張有向圖,每條邊有它的權值,要求選定一條路線,保證所經過的邊權值嚴格遞增,輸出最長路徑。

解題思路:將邊按照權值排序,每次將相同權值的邊同時加入,維護每個點作為終止點的最大長度即可。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 3 * 1e5+5;struct edge {    int u, v, w;}s[maxn];bool cmp (const edge& a, const edge& b) {    return a.w < b.w;}int n, m, d[maxn], f[maxn], val[maxn];int main () {    scanf("%d%d", &n, &m);    memset(d, 0, sizeof(d));    memset(f, 0, sizeof(f));    memset(val, 0, sizeof(val));    for (int i = 0; i < m; i++)        scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].w);    sort(s, s + m, cmp);    for (int i = 0; i < m; i++) {        int j;        for (j = i; s[j].w == s[i].w && j < m; j++);        for (int k = i; k < j; k++)            d[s[k].v] = max(d[s[k].v], f[s[k].u] + 1);        for (int k = i; k < j; k++)            f[s[k].v] = d[s[k].v];        i = j - 1;    }    int ans = 0;    for (int i = 1; i <= n; i++)        ans = max(ans, f[i]);    printf("%d\n", ans);    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.