結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2015-08-10 19:45:02 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 7 ms / 5,000 ms |
| コード長 | 1,978 bytes |
| コンパイル時間 | 824 ms |
| コンパイル使用メモリ | 96,180 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-18 06:34:00 |
| 合計ジャッジ時間 | 1,698 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
const int Vmax = 200;
int main(void){
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::fixed;//
//cout << setprecision(16);//
int N,M,start,goal;
cin>>N>>M>>start>>goal;
int d[Vmax][Vmax];
int cost[Vmax][Vmax];
const int INF = (int)1e8/2;
rep(i,Vmax)rep(j,Vmax){
d[i][j] = INF;
cost[i][j] = INF;
}
rep(i,N){
d[i][i] = 0;
cost[i][i] = 0;
}
int a,b,c;
rep(i,M){
cin>>a>>b>>c;
d[a][b] = c;
d[b][a] = c;
cost[a][b] = c;
cost[b][a] = c;
}
/*
rep(i,N)rep(j,N)rep(k,N){
d[j][k] = min( d[j][k],d[j][i]+d[i][k] );
}
*/
int dist[Vmax];
fill_n(dist, Vmax, INF);
bool use[Vmax];
fill_n(use, Vmax, false);
priority_queue<PII, vector<PII>,greater<PII>> q;
q.push( PII(0,goal) );
PII temp;
while( !q.empty() ){
temp = q.top();q.pop();
if( use[temp.second] == true ) continue;
use[temp.second] = true;
dist[temp.second] = temp.first;
for(int i=0;i<N;++i){
if( use[i] == true ) continue;
q.push( PII(temp.first + cost[temp.second][i],i) );
}
}
/////////
int now;
now = start;
vector<int> ans;
ans.push_back(now);
bool flag=true;
while(flag)
{
flag = false;
rep(i,N){
if( now == i)continue;
if( cost[now][i]+dist[i] == dist[now] ){
now = i;
flag = true;
ans.push_back(now);
break;
}
}
}
//ans.push_back(goal);
vector<int>::iterator it = ans.begin();
for(;it != ans.end() && cout<<" ";++it){
cout << *it;
}cout << endl;
return 0;
}
IL_msta