답안 #581071

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581071 2022-06-22T08:58:27 Z 조영욱(#8362) 통행료 (APIO13_toll) C++17
56 / 100
2500 ms 18528 KB
#include <bits/stdc++.h>
using namespace std;

int n,m,k;
typedef pair<int,int> P;
typedef pair<int,P> iP;
vector<iP> edge;
vector<P> vec;
int arr[20];
int p[100000];
vector<P> e;
vector<int> adj[100000];
int depth[100000];
long long sz[100000];
int people[100000];

int find(int a) {
    return p[a]<0?a:p[a]=find(p[a]);
}

void merge(int a,int b) {
    a=find(a);
    b=find(b);
    if (a==b) {
        return;
    }
    p[b]=a;
}

void dfs(int v,int prev) {
    sz[v]=people[v];
    for(int i=0;i<adj[v].size();i++) {
        int nt=adj[v][i];
        if (nt!=prev) {
            depth[nt]=depth[v]+1;
            dfs(nt,v);
            sz[v]+=sz[nt];
        }
    }
}

int main(void) {
    scanf("%d %d %d",&n,&m,&k);
    for(int i=0;i<m;i++) {
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);
        a--;
        b--;
        edge.push_back(iP(c,P(a,b)));
    }
    for(int i=0;i<k;i++) {
        int u,v;
        scanf("%d %d",&u,&v);
        u--;
        v--;
        vec.push_back(P(u,v));
    }
    for(int i=0;i<n;i++) {
        scanf("%d",&people[i]);
    }
    sort(edge.begin(),edge.end());
    long long ret=0;
    for(int bit=0;bit<(1<<k);bit++) {
        e.clear();
        memset(p,-1,sizeof(p));
        memset(arr,-1,sizeof(arr));
        for(int i=0;i<k;i++) {
            if (bit&(1<<i)) {
                merge(vec[i].first,vec[i].second);
            }
        }
        for(int i=0;i<edge.size();i++) {
            if (find(edge[i].second.first)!=find(edge[i].second.second)) {
                merge(edge[i].second.first,edge[i].second.second);
                bool flag=true;
                if (flag) {
                    e.push_back(edge[i].second);
                }
            }
        }
        for(int i=0;i<k;i++) {
            if (bit&(1<<i)) {
                memset(p,-1,sizeof(p));
                for(int j=0;j<k;j++) {
                    if (bit&(1<<j)) {
                        if (i!=j) {
                            merge(vec[j].first,vec[j].second);
                        }
                    }
                }
                for(int j=0;j<edge.size();j++) {
                    merge(edge[j].second.first,edge[j].second.second);
                    if (find(vec[i].first)==find(vec[i].second)) {
                        arr[i]=edge[j].first;
                        break;
                    }
                }
            }
        }
        int s=0;
        for(int i=0;i<k;i++) {
            if (bit&(1<<i)) {
                s++;
            }
        }
        if (e.size()+s!=n-1) {
            continue;
        }
        for(int i=0;i<n;i++) {
            adj[i].clear();
        }
        memset(p,-1,sizeof(p));
        bool flag=true;
        for(int i=0;i<e.size();i++) {
            if (find(e[i].first)==find(e[i].second)) {
                flag=false;
            }
            adj[e[i].first].push_back(e[i].second);
            adj[e[i].second].push_back(e[i].first);
            merge(e[i].first,e[i].second);
        }
        for(int i=0;i<k;i++) {
            if (bit&(1<<i)) {
                if (find(vec[i].first)==find(vec[i].second)) {
                    flag=false;
                }
                adj[vec[i].first].push_back(vec[i].second);
                adj[vec[i].second].push_back(vec[i].first);
                merge(vec[i].first,vec[i].second);
            }
        }
        if (!flag) {
            continue;
        }
        dfs(0,-1);
        long long val=0;
        for(int i=0;i<k;i++) {
            if (bit&(1<<i)) {
                int u=vec[i].first;
                if (depth[vec[i].second]>depth[vec[i].first]) {
                    u=vec[i].second;
                }
                val+=1LL*sz[u]*arr[i];
            }
        }
        ret=max(ret,val);
    }
    printf("%lld",ret);
}

Compilation message

toll.cpp: In function 'void dfs(int, int)':
toll.cpp:32:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |     for(int i=0;i<adj[v].size();i++) {
      |                 ~^~~~~~~~~~~~~~
toll.cpp: In function 'int main()':
toll.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         for(int i=0;i<edge.size();i++) {
      |                     ~^~~~~~~~~~~~
toll.cpp:91:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |                 for(int j=0;j<edge.size();j++) {
      |                             ~^~~~~~~~~~~~
toll.cpp:106:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  106 |         if (e.size()+s!=n-1) {
      |             ~~~~~~~~~~^~~~~
toll.cpp:114:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |         for(int i=0;i<e.size();i++) {
      |                     ~^~~~~~~~~
toll.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     scanf("%d %d %d",&n,&m,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         scanf("%d %d %d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
toll.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         scanf("%d %d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~~
toll.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         scanf("%d",&people[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 76 ms 3028 KB Output is correct
4 Correct 102 ms 3044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 76 ms 3028 KB Output is correct
4 Correct 102 ms 3044 KB Output is correct
5 Correct 385 ms 3288 KB Output is correct
6 Correct 138 ms 3296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 76 ms 3028 KB Output is correct
4 Correct 102 ms 3044 KB Output is correct
5 Correct 385 ms 3288 KB Output is correct
6 Correct 138 ms 3296 KB Output is correct
7 Execution timed out 2559 ms 18528 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 76 ms 3028 KB Output is correct
4 Correct 102 ms 3044 KB Output is correct
5 Correct 385 ms 3288 KB Output is correct
6 Correct 138 ms 3296 KB Output is correct
7 Execution timed out 2559 ms 18528 KB Time limit exceeded
8 Halted 0 ms 0 KB -