답안 #120833

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120833 2019-06-25T15:28:19 Z duality 통행료 (APIO13_toll) C++11
56 / 100
651 ms 27312 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

struct edge { int a,b,c; };
bool comp(edge a,edge b) {
    return a.c < b.c;
}
edge edges[300000];
int x[20],y[20];
int P[100000];
int ds[100000];
int find(int n) {
    if (ds[n] != n) ds[n] = find(ds[n]);
    return ds[n];
}
vpii adjList[100000];
int yes[100000];
int parent[100000][17],mm[100000][17],depth[100000];
LLI size[100000];
int doDFS(int u,int p,int d) {
    int i;
    parent[u][0] = p,mm[u][0] = 1e9,depth[u] = d;
    size[u] = P[u];
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i].first;
        if (v != p) doDFS(v,u,d+1),size[u] += size[v];
    }
    return 0;
}
int main() {
    int i;
    int N,M,K;
    scanf("%d %d %d",&N,&M,&K);
    for (i = 0; i < M; i++) {
        scanf("%d %d %d",&edges[i].a,&edges[i].b,&edges[i].c);
        edges[i].a--,edges[i].b--;
    }
    for (i = 0; i < K; i++) scanf("%d %d",&x[i],&y[i]),x[i]--,y[i]--;
    for (i = 0; i < N; i++) scanf("%d",&P[i]);
    sort(edges,edges+M,comp);

    int j,k;
    LLI ans = 0;
    for (i = 1; i < (1 << K); i++) {
        for (j = 0; j < N; j++) ds[j] = j;
        for (j = 0; j < K; j++) {
            if (i & (1 << j)) {
                int u = x[j],v = y[j];
                if (find(u) == find(v)) break;
                else {
                    ds[find(u)] = find(v);
                    adjList[u].pb(mp(v,1));
                    adjList[v].pb(mp(u,1));
                }
            }
        }
        if (j == K) {
            for (j = 0; j < M; j++) {
                int u = edges[j].a,v = edges[j].b;
                if (find(u) != find(v)) {
                    ds[find(u)] = find(v);
                    adjList[u].pb(mp(v,0));
                    adjList[v].pb(mp(u,0));
                    yes[j] = 0;
                }
                else yes[j] = 1;
            }
            doDFS(0,-1,0);
            for (j = 1; (1 << j) < N; j++) {
                for (k = 0; k < N; k++) {
                    if (parent[k][j-1] != -1) parent[k][j] = parent[parent[k][j-1]][j-1];
                    else parent[k][j] = -1;
                    mm[k][j] = 1e9;
                }
            }
            int logn = j;
            for (j = 0; j < M; j++) {
                int u = edges[j].a,v = edges[j].b;
                if (yes[j]) {
                    if (depth[u] < depth[v]) swap(u,v);
                    for (k = logn-1; k >= 0; k--) {
                        if ((parent[u][k] != -1) && (depth[parent[u][k]] >= depth[v]))
                            mm[u][k] = min(mm[u][k],edges[j].c),u = parent[u][k];
                    }
                    if (u != v) {
                        for (k = logn-1; k >= 0; k--) {
                            if (parent[u][k] != parent[v][k]) {
                                mm[u][k] = min(mm[u][k],edges[j].c),u = parent[u][k];
                                mm[v][k] = min(mm[v][k],edges[j].c),v = parent[v][k];
                            }
                        }
                        mm[u][0] = min(mm[u][0],edges[j].c);
                        mm[v][0] = min(mm[v][0],edges[j].c);
                    }
                }
            }
            for (j = logn-1; j >= 1; j--) {
                for (k = 0; k < N; k++) {
                    if (parent[k][j] != -1) {
                        mm[k][j-1] = min(mm[k][j-1],mm[k][j]);
                        mm[parent[k][j-1]][j-1] = min(mm[parent[k][j-1]][j-1],mm[k][j]);
                    }
                }
            }
            LLI sum = 0;
            for (j = 0; j < N; j++) {
                for (k = 0; k < adjList[j].size(); k++) {
                    int v = adjList[j][k].first;
                    if ((v != parent[j][0]) && adjList[j][k].second) sum += mm[v][0]*size[v];
                }
            }
            ans = max(ans,sum);
        }
        for (j = 0; j < N; j++) adjList[j].clear();
    }
    printf("%lld\n",ans);

    return 0;
}

Compilation message

toll.cpp: In function 'int doDFS(int, int, int)':
toll.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
toll.cpp: In function 'int main()':
toll.cpp:161:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (k = 0; k < adjList[j].size(); k++) {
                             ~~^~~~~~~~~~~~~~~~~~~
toll.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d",&N,&M,&K);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d",&edges[i].a,&edges[i].b,&edges[i].c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:92:62: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < K; i++) scanf("%d %d",&x[i],&y[i]),x[i]--,y[i]--;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
toll.cpp:93:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&P[i]);
                             ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 7 ms 2688 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 7 ms 2688 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 651 ms 3200 KB Output is correct
6 Correct 319 ms 3112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 7 ms 2688 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 651 ms 3200 KB Output is correct
6 Correct 319 ms 3112 KB Output is correct
7 Runtime error 182 ms 27312 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 7 ms 2688 KB Output is correct
4 Correct 6 ms 2816 KB Output is correct
5 Correct 651 ms 3200 KB Output is correct
6 Correct 319 ms 3112 KB Output is correct
7 Runtime error 182 ms 27312 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Halted 0 ms 0 KB -