結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー kotatsugamekotatsugame
提出日時 2020-12-17 03:40:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 618 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 84,564 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 10:34:29
合計ジャッジ時間 10,053 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 56 ms
4,348 KB
testcase_08 AC 75 ms
4,348 KB
testcase_09 AC 546 ms
4,348 KB
testcase_10 AC 96 ms
4,348 KB
testcase_11 AC 421 ms
4,348 KB
testcase_12 AC 171 ms
4,348 KB
testcase_13 AC 275 ms
4,348 KB
testcase_14 AC 13 ms
4,348 KB
testcase_15 AC 115 ms
4,348 KB
testcase_16 AC 49 ms
4,348 KB
testcase_17 AC 37 ms
4,348 KB
testcase_18 AC 31 ms
4,348 KB
testcase_19 AC 254 ms
4,348 KB
testcase_20 AC 11 ms
4,348 KB
testcase_21 AC 408 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 30 ms
4,348 KB
testcase_29 AC 304 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 51 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 430 ms
4,348 KB
testcase_34 AC 190 ms
4,348 KB
testcase_35 AC 22 ms
4,348 KB
testcase_36 AC 15 ms
4,348 KB
testcase_37 AC 4 ms
4,348 KB
testcase_38 AC 33 ms
4,348 KB
testcase_39 AC 6 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 278 ms
4,348 KB
testcase_44 AC 139 ms
4,348 KB
testcase_45 AC 618 ms
4,348 KB
testcase_46 AC 61 ms
4,348 KB
testcase_47 AC 301 ms
4,348 KB
testcase_48 AC 264 ms
4,348 KB
testcase_49 AC 31 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 1 ms
4,348 KB
testcase_52 AC 51 ms
4,348 KB
testcase_53 AC 30 ms
4,348 KB
testcase_54 AC 262 ms
4,348 KB
testcase_55 AC 266 ms
4,348 KB
testcase_56 AC 266 ms
4,348 KB
testcase_57 AC 442 ms
4,348 KB
testcase_58 AC 437 ms
4,348 KB
testcase_59 AC 432 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   43 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int T,N,M;
int U[2000],V[2000],W[2000];
bool ex[2000];
vector<long>dijkstra(int st,int ct)
{
	vector<vector<pair<int,int> > >G(N);
	for(int i=0;i<M;i++)if(ex[i])
	{
		G[U[i]].push_back(make_pair(V[i],W[i]));
		if(T==0)
		{
			G[V[i]].push_back(make_pair(U[i],W[i]));
		}
	}
	vector<long>ret(N,(long)1e18);
	priority_queue<pair<long,int> >P;
	ret[st]=ct;
	P.push(make_pair(-ct,st));
	while(!P.empty())
	{
		long cost=-P.top().first;
		int u=P.top().second;
		P.pop();
		if(ret[u]<cost)continue;
		for(pair<int,int>e:G[u])
		{
			int v=e.first;
			long nxt=cost+e.second;
			if(ret[v]>nxt)
			{
				ret[v]=nxt;
				P.push(make_pair(-nxt,v));
			}
		}
	}
	return ret;
}
main()
{
	cin>>T>>N>>M;
	for(int i=0;i<M;i++)
	{
		cin>>U[i]>>V[i]>>W[i];
		U[i]--,V[i]--;
	}
	long ans=1e18;
	for(int i=0;i<M;i++)
	{
		for(int j=0;j<M;j++)ex[j]=true;
		ex[i]=false;
		ans=min(ans,dijkstra(V[i],W[i])[U[i]]);
	}
	cout<<(ans<(long)1e18?ans:-1)<<endl;
}
0