Submission #96236

#TimeUsernameProblemLanguageResultExecution timeMemory
96236Retro3014Toll (APIO13_toll)C++17
16 / 100
4 ms2680 KiB
#include <iostream> #include <vector> #include <algorithm> #include <stdio.h> using namespace std; #define MAX_N 100000 #define MAX_M 300000 #define MAX_K 20 typedef long long ll; int N, M, K; struct EDGE{ EDGE (int x, int y, int z) : x(x), y(y), z(z) {} int x, y, z; bool operator <(const EDGE &a)const{ return z<a.z; } }; vector<EDGE> edge; struct EDGE2{ EDGE2 (int x, int y) : x(x), y(y) {} int x, y; }; vector<EDGE2> edge2; vector<int> P; vector<int> gp[MAX_N+1]; int g[MAX_N+1]; void init(){ for(int i=1; i<=N; i++) g[i] = i; } int find_g(int x){ if(x==g[x]) return x; return g[x] = find_g(g[x]); } void union_g(int x, int y){ x = find_g(x); y = find_g(y); g[x] = y; } bool use[MAX_M+1]; void solve1(){ int idx = -1; init(); for(int i=0; i<edge.size(); i++){ if(find_g(edge[i].x)==find_g(edge[i].y)) continue; union_g(edge[i].x, edge[i].y); use[i] = true; if(idx==-1 && find_g(edge2[0].x)==find_g(edge2[0].y)){ idx = i; use[i] = false; } } //cout<<idx<<endl; init(); for(int i=0; i<edge.size(); i++){ if(!use[i]) continue; union_g(edge[i].x, edge[i].y); } ll ans=0; for(int i=1; i<=N; i++){ if(find_g(i)==find_g(1)) continue; ans += (ll)P[i]; } ans *= (ll)edge[idx].z; printf("%lld", ans);} bool chk[MAX_K+1]; int p[MAX_N+1]; void dfs(int x){ for(int i=0; i<gp[x].size(); i++){ if(gp[x][i]==p[x]) continue; p[gp[x][i]] = x; dfs(gp[x][i]); } } vector<int> idx; ll answer; ll dfs2(int x){ ll sum = P[x]; for(int i=0; i<gp[x].size(); i++){ if(gp[x][i]==p[x]) continue; sum+=dfs2(gp[x][i]); } for(int i=0; i<idx.size(); i++){ if(edge[idx[i]].y==x){ answer += (ll)edge[idx[i]].z * sum; } } return sum; } void solve2(){ init(); for(int i=0; i<edge.size(); i++){ if(find_g(edge[i].x)==find_g(edge[i].y)) continue; union_g(edge[i].x, edge[i].y); use[i] = true; for(int j=0; j<K; j++){ if(!chk[j] && find_g(edge2[j].x)==find_g(edge2[j].y)){ idx.push_back(i); break; } } for(int j=0; j<K; j++){ if(!chk[j] && find_g(edge2[j].x)==find_g(edge2[j].y)){ chk[j] = true; } } } //init(); for(int i=0; i<edge.size(); i++){ if(!use[i]) continue; gp[edge[i].x].push_back(edge[i].y); gp[edge[i].y].push_back(edge[i].x); } dfs(1); for(int i=0; i<edge.size(); i++){ if(!use[i]) continue; union_g(edge[i].x, edge[i].y); } for(int i=0; i<idx.size(); i++){ if(edge[idx[i]].y == p[edge[idx[i]].x]){ int tmp = edge[idx[i]].y; edge[idx[i]].y = edge[idx[i]].x; edge[idx[i]].x = tmp; } } dfs2(1); printf("%lld", answer); } int main(){ 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); edge.push_back({a, b, c}); }sort(edge.begin(), edge.end()); for(int i=0; i<K; i++){ int a, b; scanf("%d%d", &a, &b); edge2.push_back({a, b}); }P.push_back(0); for(int i=1; i<=N; i++){ int a; scanf("%d", &a); P.push_back(a); } //if(N<=10 && M<=20 && K==1) solve1(); solve2(); return 0; }

Compilation message (stderr)

toll.cpp: In function 'void solve1()':
toll.cpp:49:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge.size(); i++){
               ~^~~~~~~~~~~~
toll.cpp:60:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge.size(); i++){
               ~^~~~~~~~~~~~
toll.cpp: In function 'void dfs(int)':
toll.cpp:76:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
toll.cpp: In function 'll dfs2(int)':
toll.cpp:86:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<gp[x].size(); i++){
               ~^~~~~~~~~~~~~
toll.cpp:90:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<idx.size(); i++){
               ~^~~~~~~~~~~
toll.cpp: In function 'void solve2()':
toll.cpp:100:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge.size(); i++){
               ~^~~~~~~~~~~~
toll.cpp:117:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge.size(); i++){
               ~^~~~~~~~~~~~
toll.cpp:122:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<edge.size(); i++){
               ~^~~~~~~~~~~~
toll.cpp:126:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<idx.size(); i++){
               ~^~~~~~~~~~~
toll.cpp: In function 'int main()':
toll.cpp:136:7: 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:139:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:144:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b); edge2.push_back({a, b});
   ~~~~~^~~~~~~~~~~~~~~~
toll.cpp:147:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a; scanf("%d", &a); P.push_back(a);
          ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...