結果

問題 No.114 遠い未来
ユーザー 👑 kmjpkmjp
提出日時 2014-12-28 23:39:31
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,873 bytes
コンパイル時間 1,295 ms
コンパイル使用メモリ 152,932 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 18:50:12
合計ジャッジ時間 2,410 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 1 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 2 ms
4,380 KB
testcase_14 WA -
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 WA -
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

class UF {
	public:
	static const int ufmax=5052;
	int ufpar[ufmax],ufrank[ufmax],ufcnt[ufmax];
	UF() { int i; FOR(i,ufmax) { ufpar[i]=i; ufrank[i]=0; ufcnt[i]=1; } }
	int operator[](int x) {return (ufpar[x]==x)?(x):(ufpar[x] = operator[](ufpar[x]));}
	int count(int x) { return ufcnt[operator[](x)];}
	void unite(int x,int y) {
		x = operator[](x); y = operator[](y);
		if(x==y) return;
		if(ufrank[x]<ufrank[y]) ufpar[x]=y, ufcnt[y]+=ufcnt[x];
		else {ufpar[y]=x; ufcnt[x]+=ufcnt[y]; if(ufrank[x]==ufrank[y]) ufrank[x]++;}
	}
};

int N,M,T;
int A[2000],B[2000],C[2000];
int V[2000];
int mat[40][40];

int ok[40];

void solve() {
	int i,j,k,l,r,x,y; string s;
	UF uf;
	
	FOR(x,40) FOR(y,40) mat[x][y]=100000000;
	FOR(x,40) mat[x][x]=0;
	cin>>N>>M>>T;
	FOR(i,M) {
		cin>>A[i]>>B[i]>>C[i];
		mat[A[i]-1][B[i]-1]=mat[B[i]-1][A[i]-1]=C[i];
	}
	FOR(i,N) FOR(x,N) FOR(y,N) mat[x][y]=min(mat[x][y],mat[x][i]+mat[i][y]);
	FOR(i,T) cin>>V[i], V[i]--, ok[V[i]]=1;
	priority_queue<pair<int,int> > Q;
	FOR(x,N) FOR(y,N) if(x!=y && ok[x]&&ok[y]) Q.push(make_pair(-mat[x][y],x*100+y));
	
	int tot=0;
	while(Q.size()) {
		pair<int,int> p=Q.top();
		Q.pop();
		x=p.second/100, y=p.second%100;
		if(uf[x]!=uf[y]) {
			tot+=-p.first;
			uf.unite(x,y);
		}
	}
	cout<<tot<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0