hihocoder #1185 : 連通性·三 tarjan

來源:互聯網
上載者:User

標籤:ext   c++   ble   強連通分量   暑假   title   ack   stack   include   

#1185 : 連通性·三時間限制:10000ms單點時限:1000ms記憶體限制:256MB描述

暑假到了!!小Hi和小Ho為了體驗生活,來到了住在大草原的約翰家。今天一大早,約翰因為有事要出去,就拜託小Hi和小Ho忙幫放牧。

約翰家一共有N個草場,每個草場有容量為W[i]的牧草,N個草場之間有M條單向的路徑。

小Hi和小Ho需要將牛羊群趕到草場上,當他們吃完一個草場牧草後,繼續前往其他草場。當沒有可以到達的草場或是能夠到達的草場都已經被吃光了之後,小hi和小Ho就把牛羊群趕回家。

一開始小Hi和小Ho在1號草場,在回家之前,牛羊群最多能吃掉多少牧草?

舉個例子:

圖中每個點表示一個草場,上部分數字表示編號,下部分表示草場的牧草數量w。

在1吃完草之後,小Hi和小Ho可以選擇把牛羊群趕到2或者3,假設小Hi和小Ho把牛羊群趕到2:

吃完草場2之後,只能到草場4,當4吃完後沒有可以到達的草場,所以小Hi和小Ho就把牛羊群趕回家。

若選擇從1到3,則可以到達5,6:

選擇5的話,吃完之後只能直接回家。若選擇6,還可以再通過6回到3,再到5。

所以該圖可以選擇的路線有3條:

1->2->4 total: 111->3->5 total: 91->3->6->3->5: total: 13  

所以最多能夠吃到的牧草數量為13。

 

 

本題改編自USACO月賽金組

提示:強連通分量

輸入

第1行:2個正整數,N,M。表示點的數量N,邊的數量M。1≤N≤20,000, 1≤M≤100,000

第2行:N個正整數,第i個整數表示第i個牧場的草量w[i]。1≤w[i]≤100,000

第3..M+2行:2個正整數,u,v。表示存在一條從u到v的單向路徑。1≤u,v≤N

輸出

第1行:1個整數,最多能夠吃到的牧草數量。

 

範例輸入
6 62 4 3 5 4 41 22 41 33 53 66 3
範例輸出 13
跑一遍tarjan,將同一個強連通變數裡的點縮起來,建立一張新圖,跑dfs。
#include <bits/stdc++.h>using namespace std;#define ll long long#define clr(a,b) memset(a,b,sizeof(a))#define fir first#define se second#define pb(x) push_back(x)#define ALL(V) (V).begin(), (V).end()const int maxn = 1e5 + 10;const int mod = 1000000007;const int oo = 0x3f3f3f3f;typedef pair<int, int> pii;typedef pair<ll, ll> pll;typedef pair<double, int> pdi;typedef vector<int> vii;//header 1.0int n, m, t, id;int a[maxn];int na[maxn];stack<int> s;vii g[maxn], rg[maxn];vii ng[maxn];vii ans[maxn];int chk[maxn];int grp[maxn];void init() {    for(int i = 0; i < maxn; i++) g[i].clear();    for(int i = 0; i < maxn; i++) rg[i].clear();    for(int i = 0; i < maxn; i++) ng[i].clear();    for(int i = 0; i < maxn; i++) ans[i].clear();    clr(a, 0); clr(na, 0);    clr(chk, 0); clr(grp, 0);    while(!s.empty()) s.pop();    id = 0;}void dfs(int x) {    if(chk[x] == t) return;    chk[x] = t;    for(int i : g[x]) dfs(i);    s.push(x);}void dfs2(int x) {    if(chk[x] == t) return;    chk[x] = t; ans[id].pb(x);    grp[x] = id;    for(int i : rg[x]) dfs2(i);}int fans;int vis[maxn];void dfs3(int x, int sum) {    fans = max(fans, sum);    for(int i : ng[x]) {        dfs3(i, sum + na[i]);    }}int main() {    //freopen("d:/in.txt", "r", stdin);    //freopen("d:/out2.txt", "w", stdout);    while(~scanf("%d %d", &n, &m)) {        init();        for(int i = 1; i <= n; i++) {            scanf("%d", &a[i]);        }        for(int i = 1; i <= m; i++) {            int u, v;            scanf("%d %d", &u, &v);            g[u].pb(v), rg[v].pb(u);        }        t = 1;        for(int i = 1; i <= n; i++) {            if(chk[i] != t) {                dfs(i);            }        }        t = 2;        for(int i = 1; i <= n; i++) {            int tmp = s.top();            if(chk[tmp] != t) {                ++id;                dfs2(tmp);            }            s.pop();        }        for(int i = 1; i <= id; i++) {            int sum = 0;            for(int j : ans[i]) {                for(int k : g[j]) {                    if(grp[k] != i)                    ng[i].pb(grp[k]);                }                for(int k : rg[j]) {                    if(grp[k] != i)                    ng[grp[k]].pb(i);                }                sum += a[j];            }            na[i] = sum;        }        fans = 0;        clr(vis, 0);        //printf("check %d %d\n", grp[2], a[grp[2]]);        dfs3(grp[1], na[grp[1]]);        printf("%d\n", fans);    }}

 




hihocoder #1185 : 連通性·三 tarjan

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.