답안 #580989

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
580989 2022-06-22T07:45:52 Z 조영욱(#8362) 통행료 (APIO13_toll) C++17
16 / 100
3 ms 3072 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));
    }
    sort(edge.begin(),edge.end());
    memset(p,-1,sizeof(p));
    memset(arr,-1,sizeof(arr));
    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;
            for(int j=0;j<k;j++) {
                if (arr[j]==-1) {
                    if (find(vec[j].first)==find(vec[j].second)) {
                        arr[j]=edge[i].first;
                        flag=false;
                    }
                }
            }
            if (flag) {
                e.push_back(edge[i].second);
            }
        }
    }
    for(int i=0;i<n;i++) {
        scanf("%d",&people[i]);
    }
    long long ret=0;
    for(int bit=0;bit<(1<<k);bit++) {
        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:61:18: 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]
   61 |     for(int i=0;i<edge.size();i++) {
      |                 ~^~~~~~~~~~~~
toll.cpp:89: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]
   89 |         if (e.size()+s!=n-1) {
      |             ~~~~~~~~~~^~~~~
toll.cpp:97: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]
   97 |         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:79:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         scanf("%d",&people[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Incorrect 3 ms 3028 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Incorrect 3 ms 3028 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Incorrect 3 ms 3028 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3072 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Incorrect 3 ms 3028 KB Output isn't correct
4 Halted 0 ms 0 KB -