1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 200005 #define put() putchar('\n') #define Tp template<typename T> #define Ts template<typename T,typename... Ar> using namespace std; Tp void read(T &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,m,k,siz[maxn]; int dfn[maxn],low[maxn],tot,vis[maxn],cnt,times; pair<int,int>stac[maxn]; int h[maxn],head=1; struct yyy { int to,z,w; void add(int x,int y,int val) { to=y;z=h[x];h[x]=head;w=val; } }a[maxn*2]; vector<pair<int,int> >to[maxn]; void add(int x,int y,int w) {
to[x].push_back(mk(y,w)); to[y].push_back(mk(x,w)); } void tarjan(int x,int pre,int pw) { int i,j; low[x]=dfn[x]=++times,stac[++tot]=mk(x,pw),vis[x]=1; for (i=h[x];i;i=a[i].z) if ((i^1)^pre) { if (!dfn[a[i].to]) { tarjan(a[i].to,i,a[i].w),low[x]=min(low[x],low[a[i].to]); int y=a[i].to; if (low[y]>=dfn[x]) { ++cnt; int u=stac[tot].fi; while (1) { add(cnt,stac[tot].fi,stac[tot].se); vis[stac[tot].fi]=0; if (stac[tot--].fi==a[i].to) break; } int res=0; for (j=h[u];j;j=a[j].z) {
if (a[j].to==x) res=a[j].w; } add(cnt,x,res);
} } else if (vis[a[i].to]) { low[x]=min(low[x],dfn[a[i].to]); } } } int ans; void dfs(int x,int pre) { for (auto y:to[x]) if (y.fi^pre) dfs(y.fi,x),siz[x]+=siz[y.fi]; } int id[maxn],s[maxn]; pair<int,int>r[maxn]; const int inf=1e14; void calc(int x,int pre) { int i,j,res=0; tot=0; for (auto tmp:to[x]) { id[++tot]=tmp.se; s[tot]=(tmp.fi==pre?-siz[x]:siz[tmp.fi]); } for (i=1;i<=tot;i++) id[i+tot]=id[i],s[i+tot]=s[i]; int tmp=0,sum=0; for (i=1;i<=tot;i++) r[i]=mk(r[i-1].fi+s[i],id[i]),tmp+=id[i]; tmp=(tmp+1)/2; sort(r+1,r+1+tot); for (i=1;i<=tot;i++) if (r[i].se<tmp) tmp-=r[i].se; else {res=r[i].fi;break;} for (i=1;i<=tot;i++) sum+=r[i].se*abs(res-r[i].fi); ans+=sum;
} void dfs2(int x,int pre) { for (auto tmp:to[x]) if (tmp.fi^pre) { int y=tmp.fi; dfs2(y,x); if (x<=n&&y<=n) ans+=abs(siz[y])*tmp.se; } if (x>n) { calc(x,pre); } } map<pair<int,int> ,int>mp; signed main(void){ int i,x,y,z; read(n);read(m);read(k);cnt=n; for (i=1;i<=k;i++) read(x),siz[x]++; for (i=1;i<=k;i++) read(x),siz[x]--; for (i=1;i<=m;i++) { read(x),read(y),read(z); if (x>y) swap(x,y); if (!mp[mk(x,y)]) mp[mk(x,y)]=z; else mp[mk(x,y)]=min(mp[mk(x,y)],z); } for (auto tmp:mp) { a[++head].add(tmp.fi.fi,tmp.fi.se,tmp.se); a[++head].add(tmp.fi.se,tmp.fi.fi,tmp.se); } for (i=1;i<=n;i++) if (!dfn[i]) tarjan(i,0,0); dfs(1,0); dfs2(1,0); printf("%lld",ans); return 0; }
|