結果

問題 No.2604 Initial Motion
ユーザー TairitsuMeowTairitsuMeow
提出日時 2024-01-12 21:58:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,304 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 179,436 KB
実行使用メモリ 150,368 KB
最終ジャッジ日時 2024-01-12 21:58:43
合計ジャッジ時間 16,668 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 1,014 ms
8,492 KB
testcase_04 AC 927 ms
8,488 KB
testcase_05 AC 1,055 ms
8,492 KB
testcase_06 AC 1,013 ms
8,492 KB
testcase_07 AC 993 ms
8,492 KB
testcase_08 AC 1,058 ms
8,492 KB
testcase_09 AC 980 ms
8,488 KB
testcase_10 AC 1,044 ms
8,488 KB
testcase_11 AC 997 ms
8,488 KB
testcase_12 AC 1,050 ms
8,492 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// Problem: No.2604 Initial Motion
// Contest: yukicoder
// URL: https://yukicoder.me/problems/no/2604
// Memory Limit: 512 MB
// Time Limit: 3000 ms

#include<bits/stdc++.h>
#define debug(x) cerr<<(#x)<<" "<<(x)<<endl
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pii pair<ll,ll>
#define rep(i,a,b) for(ll i=(a);i<=(b);++i)
#define per(i,a,b) for(ll i=(a);i>=(b);--i)
using namespace std;
ll read(){
	ll x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
const ll N=1e4+5,M=5e6+5,INF=1e14;
ll n,m,S,T,tot=1,hd[N],mxf,mnc,d[N],pre_p[N],pre_e[N],flow[N],in[N];
ll dis[N],ok[N],a[N],b[N],k;
struct Edg{
	ll to,nxt,c,f;
}es[M<<1];
void adde(ll x,ll y,ll c,ll f){
	es[++tot]=(Edg){y,hd[x],c,f};
	hd[x]=tot;
	es[++tot]=(Edg){x,hd[y],-c,0};
	hd[y]=tot;
}
bool spfa(){
	rep(i,0,T)d[i]=flow[i]=INF,in[i]=0;
	queue<ll>q;
	q.push(S);
	d[S]=0,flow[S]=INF,pre_p[T]=-1,in[S]=1;
	while(!q.empty()){
		ll u=q.front();q.pop();
		in[u]=0;
		for(ll i=hd[u];i;i=es[i].nxt){
			ll v=es[i].to,c=es[i].c,f=es[i].f;
			if(f&&d[v]>d[u]+c){
				d[v]=d[u]+c;
				flow[v]=min(flow[u],f);
				pre_p[v]=u,pre_e[v]=i;
				if(!in[v])in[v]=1,q.push(v);
			}
		}
	}
	return pre_p[T]!=-1;
}
void runflow(){
	for(ll x=T;spfa();){
		for(;x!=S;x=pre_p[x])es[pre_e[x]].f-=flow[T],es[pre_e[x]^1].f+=flow[T];
		mxf+=flow[T],mnc+=d[T]*flow[T];
		x=T;
	}
}
vector<pii>to[N];
int main(){
	k=read(),n=read(),m=read();
	rep(i,1,k)a[i]=read();
	rep(i,1,n)b[i]=read();
	rep(i,1,m){
		ll x=read(),y=read(),z=read();
		to[x].push_back(make_pair(y,z));
		to[y].push_back(make_pair(x,z));
	}
	S=0,T=n+k+1;
	rep(i,1,k){
		rep(j,1,n)dis[j]=INF,ok[j]=0;
		dis[a[i]]=0;
		priority_queue<pii>q;
		q.push(make_pair(0,a[i]));
		while(!q.empty()){
			pii now=q.top();q.pop();
			ll u=now.second;
			if(ok[u])continue;
			ok[u]=1;
			for(pii es:to[u]){
				ll v=es.first,w=es.second;
				if(dis[v]>dis[u]+w){
					dis[v]=dis[u]+w;
					q.push(make_pair(-dis[v],v));
				}
			}
		}
		rep(j,1,n){
			if(b[j])adde(i,j+k,dis[j],1);
		}
		adde(S,i,0,1);
	}
	rep(i,1,n){
		if(b[i])adde(i+k,T,0,b[i]);
	}
	runflow();
	write(mnc);
	return 0;
}

0