結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
tonegawa
|
| 提出日時 | 2021-02-12 08:04:06 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,710 bytes |
| コンパイル時間 | 1,572 ms |
| コンパイル使用メモリ | 130,020 KB |
| 最終ジャッジ日時 | 2025-01-18 17:46:52 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 46 WA * 11 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:118:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
118 | int t;scanf("%d", &t);
| ~~~~~^~~~~~~~~~
main.cpp:119:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
119 | int n, m;scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:122:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
122 | int a, b, c;scanf("%d %d %d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;
/*---tips---
重み付きの有向グラフについて、重みの和が最小になるサイクルを探す O(N(N + M)logN)
自己辺は閉路と見なさない(含める場合、e.from = e.toの辺を列挙するだけでいい)
多重辺があっても良い
無向グラフに使う場合、edgeのidxにuniqueな値を持たせて逆流できないようにすればいい
verify:
*/
struct edge{
int from, to, idx;
ll w;
edge(int f, int t, int i, ll w=1):from(f), to(t), idx(i), w(w){}
};
pair<ll, vector<edge*>> mincost_cycle(vector<vector<edge>> &G){
int n = G.size();
ll INF = numeric_limits<ll>::max(), ans = INF;
vector<edge*> min_cycle;
vector<ll> dist(n);
vector<edge*> pre(n);
for(int i=0;i<n;i++){
fill(dist.begin()+i+1, dist.end(), INF);
fill(pre.begin()+i, pre.end(), nullptr);
dist[i] = 0;
using p = pair<ll, int>;
priority_queue<p, vector<p>, greater<p>> q;
q.push({0, i});
int idx = -1;
while(!q.empty()){
p p_now = q.top(); q.pop();
int v = p_now.second;
if(dist[v] == INF || dist[v] < p_now.first || ans <= dist[v]) continue;
for(edge &e:G[v]){
if(pre[v] != nullptr && pre[v]->idx == e.idx) continue; //if use undirected edge
if(e.to==i && dist[v] + e.w < ans){
ans = dist[v] + e.w;
idx = v;
pre[i] = &e;
continue;
}
if(e.to<i) continue;
if(dist[e.to] > dist[v] + e.w){
dist[e.to] = dist[v] + e.w;
pre[e.to] = &e;
q.push(p(dist[e.to], e.to));
}
}
}
if(idx != -1){
vector<edge*> lis;
idx = i;
while(pre[idx]){
lis.push_back(pre[idx]);
int k = pre[idx]->from;
pre[idx] = nullptr;
idx = k;
}
reverse(lis.begin(), lis.end());
min_cycle = lis;
}
/*if use undirected edge*/
edge *ud = nullptr;
for(int j=i+1;j<n;j++){
for(edge &e:G[j]){
if(e.to<i || dist[e.from] == INF || dist[e.to] == INF || (pre[e.to] && pre[e.to]->idx == e.idx) || (pre[e.from] && pre[e.from]->idx == e.idx)) continue;
if(ans > dist[e.from] + dist[e.to] + e.w){
ans = dist[e.from] + dist[e.to] + e.w;
ud = &e;
}
}
}
if(ud){
vector<edge*> left{ud};
int tmp = ud->from;
while(pre[tmp]&&tmp!=i){
left.push_back(pre[tmp]);
tmp = pre[tmp]->from;
}
reverse(left.begin(), left.end());
tmp = ud->to;
while(pre[tmp]&&tmp!=i){
assert(pre[tmp]);
left.push_back(pre[tmp]);
tmp = pre[tmp]->from;
}
min_cycle = left;
}
}
return {ans, min_cycle};
}
int main(){
int t;scanf("%d", &t);
int n, m;scanf("%d %d", &n, &m);
vector<vector<edge>> G(n);
for(int i=0;i<m;i++){
int a, b, c;scanf("%d %d %d", &a, &b, &c);
a--, b--;
G[a].push_back(edge(a, b, i, c));
if(t==0) G[b].push_back(edge(b, a, i, c));
}
auto ans = mincost_cycle(G);
if(ans.second.empty()) printf("%d\n", -1);
else printf("%lld\n", ans.first);
}
tonegawa