結果
| 問題 |
No.1393 Median of Walk
|
| コンテスト | |
| ユーザー |
carrot46
|
| 提出日時 | 2021-02-16 16:18:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 50 ms / 2,000 ms |
| コード長 | 2,344 bytes |
| コンパイル時間 | 1,989 ms |
| コンパイル使用メモリ | 185,384 KB |
| 実行使用メモリ | 41,472 KB |
| 最終ジャッジ日時 | 2024-09-13 03:50:43 |
| 合計ジャッジ時間 | 4,246 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
struct edge{
ll to;
int ord;
edge(){}
edge(ll a, int b) : to(a), ord(b){}
};
using ve = vector<edge>;
void bfs(vector<ve> &G, vec &dist){
dist[0] = 0;
queue<int> que;
que.push(0);
while(!que.empty()){
int v = que.front();
que.pop();
for(edge &e : G[v]){
if(chmin(dist[e.to], dist[v] + 1)){
que.push(e.to);
}
}
}
}
void dfs(ll v, ll id, vec &dist, vector<ve> &G, mat &res){
for(edge &e : G[v]){
ll new_cost = dist[v] + (e.ord <= id ? -1 : 1);
if(new_cost <= - N) new_cost = -INF;
if(dist[e.to] > new_cost){
if(dist[e.to] > 0 && new_cost <= 0) res[id].push_back(e.to);
dist[e.to] = new_cost;
dfs(e.to, id, dist, G, res);
}
}
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin>>N>>M;
vector<ve> G(N);
vec u(M), v(M), w(M), ord(M), dist(N, INF);
mat res(M);
rep(i, M){
cin>>u[i]>>v[i]>>w[i];
--u[i]; --v[i];
}
iota(ALL(ord), 0);
sort(ALL(ord), [&](int x, int y){return w[x] < w[y];});
rep(_, M) {
int i = ord[_];
G[u[i]].emplace_back(v[i], _);
}
bfs(G, dist);
//my_printv(dist);
rep(i, M) dfs(u[ord[i]], i, dist, G, res);
vec ans(N, -1);
rep(i, M) for(int j : res[i]) ans[j] = w[ord[i]];
reps(i, 1, N) cout<<ans[i]<<endl;
}
carrot46