結果
| 問題 |
No.1301 Strange Graph Shortest Path
|
| コンテスト | |
| ユーザー |
idat_50me
|
| 提出日時 | 2020-12-10 01:38:18 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 6,024 bytes |
| コンパイル時間 | 2,519 ms |
| コンパイル使用メモリ | 209,744 KB |
| 最終ジャッジ日時 | 2025-01-16 21:11:31 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 TLE * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<typename T, typename U>
using vp = vector<pair<T,U>>;
template<typename T>
using pque = priority_queue<T>;
template<typename T>
using lpque = priority_queue<T,vector<T>,greater<T>>;
using ll = long long;
using pint = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using vint = vector<int>;
using vll = vector<ll>;
using qint = queue<int>;
using pqint = pque<int>;
using qll = queue<ll>;
using pqll = pque<ll>;
constexpr double PI = 3.141592653589793;
constexpr int INTINF = (1<<30)-1;
constexpr ll LLINF = (1LL<<62)-1;
constexpr int MPRIME = 1000000007;
constexpr int MPRIME9 = 998244353;
constexpr ll MMPRIME = (1LL<<61)-1;
constexpr char newl = '\n';
#define len length()
#define pushb push_back
#define fi first
#define se second
#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())
template<typename T>
struct matrix{
private:
vector<vector<T>> mat;
public:
matrix() : matrix(0,0) {}
matrix(int h, int w) { resize(h,w); }
matrix(int h, int w, T init) { resize(h,w,init); }
void resize(int h, int w) {
mat=vector<vector<T>>(h,vector<T>(w));
}
void resize(int h, int w, T init) {
mat=vector<vector<T>>(h,vector<T>(w,init));
};
void in() {
for(int i=0; i<mat.size(); i++) for(int j=0; j<mat[i].size(); j++) {
cin>>mat[i][j];
}
}
void out() {
for(int i=0; i<mat.size(); i++) {
int wm=mat[i].size();
for(int j=0; j<wm; j++) {
cout<<mat[i][j]<<(wm==j+1 ? '\n' : ' ');
}
}
cout<<flush;
}
inline vector<T> &operator[](int idx) {
assert(0<=idx && idx<mat.size());
return mat[idx];
}
};
template<class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline void init(T& v) {
for(auto &a: v) cin>>a;
}
template<class T, class U>
inline void init(vector<pair<T,U>>& v) {
for(auto &a: v) cin>>a.first>>a.second;
}
template<class T, class N>
inline void init(T& v, N n) {
v.resize(n);
for(auto &a: v) cin>>a;
}
template<class T, class U, class N>
inline void init(vector<pair<T,U>>& v, N n) {
v.resize(n);
for(auto &a: v) cin>>a.first>>a.second;
}
template<class T>
inline void out(T a) {
cout<<a<<endl;
}
template<class T, class... U>
inline void out(T a, U... alist) {
cout<<a<<" ";
out(forward<U>(alist)...);
}
template<class N>
void resiz(N n) {
//empty
}
template<class N, class T, class... U>
void resiz(N n, T&& hd, U&&... tl) {
hd.resize(n);
resiz(n,forward<U>(tl)...);
}
long long binpow(long long a, long long ex, long long p=MMPRIME) {
long long res = 1;
while(ex > 0) {
if(ex & 1) (res*=a) %= p;
ex>>=1;
(a*=a) %= p;
}
return res;
}
struct mincostflow {
private:
struct edge {
int next;
int rev;
long long cap;
long long cost;
edge(int next, int rev, long long cap, long long cost) : next(next), rev(rev), cap(cap), cost(cost) {}
};
public:
const long long inf = (1LL<<62)-1;
private:
const int vnum;
vector<vector<edge>> G;
vector<long long> pot;
vector<int> pv, pe;
public:
mincostflow(int N) : vnum(N), G(N), pot(N), pv(N), pe(N) {}
void add(int from, int to, long long cap, long long cost) {
G[from].push_back(edge(to, G[to].size(), cap, cost));
G[to].push_back(edge(from, G[from].size()-1, 0, -cost));
}
private:
long long bellman_ford(int s, int t, int &f) {
pot.assign(vnum, inf);
pv.assign(vnum, -1);
pe.assign(vnum, -1);
pot[s] = 0;
for(int i=0; i<vnum; i++) {
for(int j=0; j<vnum; j++) {
if(pot[j] == inf) continue;
for(int k=0; k<G[j].size(); k++) {
const edge &ed = G[j][k];
if(ed.cap>0 && pot[ed.next]>pot[j]+ed.cost) {
if(i == vnum-1) return -inf;
pot[ed.next] = pot[j]+ed.cost;
pv[ed.next] = j;
pe[ed.next] = k;
}
}
}
}
int add_f = f;
for(int v=t; v!=s; v=pv[v]) add_f = min((long long)add_f, G[pv[v]][pe[v]].cap);
f -= add_f;
for(int v=t; v!=s; v=pv[v]) {
edge &ed = G[pv[v]][pe[v]];
ed.cap -= add_f;
G[v][ed.rev].cap += add_f;
}
return pot[t]*add_f;
}
long long dijkstra(int s, int t) {
long long ans = 0;
priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> q;
vector<long long> dist(vnum, inf);
pv.assign(vnum, -1);
pe.assign(vnum, -1);
q.push(make_pair(0LL,s));
dist[s] = 0;
while(!q.empty()) {
long long d = q.top().first, v = q.top().second;
q.pop();
if(dist[v] < d) continue;
for(int i=0; i<G[v].size(); i++) {
edge &ed = G[v][i];
long long nd = d+ed.cost+pot[v]-pot[ed.next];
if(ed.cap>0 && dist[ed.next]>nd) {
dist[ed.next] = nd;
pv[ed.next] = v;
pe[ed.next] = i;
q.push(make_pair(nd,ed.next));
}
}
}
if(dist[t] == inf) return inf;
ans = dist[t]+pot[t];
for(int v=0; v<vnum; v++) {
if(dist[v] == inf) continue;
pot[v] += dist[v];
}
return ans;
}
public:
// -inf: 負閉路検出 inf: 未到達
long long solve(int s, int t, int f) {
long long res = bellman_ford(s, t, f);
if(abs(res) == inf) return res;
while(f > 0) {
long long restmp = dijkstra(s, t);
int add_f = f;
if(restmp == inf) return inf;
for(int v=t; v!=s; v=pv[v]) add_f = min((long long)add_f, G[pv[v]][pe[v]].cap);
f -= add_f;
res += restmp*add_f;
for(int v=t; v!=s; v=pv[v]) {
edge &ed = G[pv[v]][pe[v]];
ed.cap -= add_f;
G[v][ed.rev].cap += add_f;
}
}
return res;
}
};
int N,M;
void input() {
cin>>N>>M;
}
void solve() {
mincostflow mcf(N+1);
for(int i=0; i<M; i++) {
int u,v,c,d; cin>>u>>v>>c>>d;
mcf.add(u,v,1,c);
mcf.add(u,v,1,d);
mcf.add(v,u,1,c);
mcf.add(v,u,1,d);
}
cout<<mcf.solve(1,N,2)<<newl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout<<fixed<<setprecision(15);
int t=1;
while(t--) {
input();
solve();
}
}
idat_50me