結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
peroon
|
| 提出日時 | 2019-04-10 22:13:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 529 ms / 4,000 ms |
| コード長 | 4,546 bytes |
| コンパイル時間 | 1,842 ms |
| コンパイル使用メモリ | 117,288 KB |
| 実行使用メモリ | 42,120 KB |
| 最終ジャッジ日時 | 2024-11-23 19:25:22 |
| 合計ジャッジ時間 | 8,116 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const int N_MAX = 200010;
struct Edge{
ll to = 0;
ll cost = 0;
Edge(ll to, ll cost): to(to), cost(cost) {}
Edge(){}
};
struct Node{
ll distance;
ll index;
bool ticket_used = false;
Node(ll dist, ll ind){
distance = dist;
index = ind;
}
Node(){}
bool operator<(const Node &another) const
{
return distance < another.distance;
}
bool operator>(const Node &another) const
{
return distance > another.distance;
}
};
struct DijkstraBase{
vector<ll> d; // 距離
vector<vector<Edge> > graph;
vector<bool> done;
// ノード数を入れる
void initialize(ll size){
d.resize(size);
done.resize(size);
graph.resize(size);
FOR(i, 0, size){
d[i] = inf;
done[i] = false;
}
}
ll distance(int i){
if(d.size()<=i) return -1;
return d[i];
}
void print_graph(){
FOR(i, 0, graph.size()){
cout << i << " -> ";
for(auto edge : graph[i]){
cout << edge.to << " ";
}
cout << endl;
}
}
void register_edge(ll a, ll b, ll cost){
auto edge = Edge(b, cost);
graph[a].push_back(edge);
}
void calc_shortest_path(){
priority_queue<Node, vector<Node>, greater<Node> > que;
auto node = Node(0, 0);
que.push(node);
while(!que.empty()){
// 1番distanceが小さいノードを取り出す
Node n = que.top();
que.pop();
if(done[n.index]) continue;
done[n.index] = true;
d[n.index] = n.distance;
auto edge_list = graph[n.index];
for(auto edge : edge_list){
// 短くなるノードがあれば
if(!done[edge.to] && n.distance + edge.cost < d[edge.to]){
ll shorter_distance = n.distance + edge.cost;
auto node = Node(shorter_distance, edge.to);
que.push(node);
}
}
}
}
};
struct Dijkstra : public DijkstraBase{
};
Dijkstra dij;
struct DijkstraTicket : public DijkstraBase{
void calc_shortest_path(){
ll N = graph.size();
FOR(i, 0, N){
d[i] = dij.distance(i);
}
priority_queue<Node, vector<Node>, greater<Node> > que;
auto node = Node(0, 0);
que.push(node);
while(!que.empty()){
Node n = que.top();
que.pop();
if(done[n.index]) continue;
done[n.index] = true;
d[n.index] = n.distance;
auto edge_list = graph[n.index];
for(auto edge : edge_list){
ll dist0 = dij.distance(n.index) + 0; // 最後にチケットを使った
ll dist1 = d[n.index] + edge.cost; // チケットを使ってiまで来て、最後の一歩は普通に
if(!done[edge.to]){
auto node0 = Node(dist0, edge.to);
auto node1 = Node(dist1, edge.to);
que.push(node0);
que.push(node1);
}
}
}
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
// input
ll N, M;
cin >> N >> M;
dij.initialize(N);
DijkstraTicket dijT;
dijT.initialize(N);
FOR(i, 0, M){
ll a, b, c;
cin >> a >> b >> c;
a--;
b--;
dij.register_edge(a, b, c);
dij.register_edge(b, a, c);
dijT.register_edge(a, b, c);
dijT.register_edge(b, a, c);
}
dij.calc_shortest_path();
dijT.calc_shortest_path();
FOR(i, 0, N){
ll ans = dij.distance(i) + dijT.distance(i);
p(ans);
}
return 0;
}
peroon