Submission #718446

#TimeUsernameProblemLanguageResultExecution timeMemory
718446KalashnikovRobot (JOI21_ho_t4)C++17
100 / 100
209 ms38168 KiB
#include <bits/stdc++.h>
 
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
 
using namespace std;
using ll = long long;
 #define int ll
const int N = 2e5+5 , inf = 2e9 + 7;
const ll INF = 1e18 ,   mod = 1e9+7 , P = 6547;

map<int,vector<pair<int,int>>> mp[N];
vector<array<int,3>> g[N];
ll sum[N];
ll mn[N];

void solve(int tc) {
	int n, m;
	cin >> n >> m;
	vector<array<int,4>> vec;
	for(int i = 1; i <= m; i ++) {
		int a, b,c, p;
		cin >> a >> b >> c >> p;
		g[a].push_back({b,c,p});
		g[b].push_back({a,c,p});
	}
	set<pair<ll,int>> st;
	vector<ll> d(n+1,INF);
	fill(mn, mn+m+1, INF);
	d[1] = 0;
	st.insert({0, 1});
	while(st.size()) {
		pair<int,int> cur = *st.begin();
		st.erase(cur);
		int v = cur.S;
		for(auto to: g[v]) {
			sum[to[1]] += to[2];
			mn[to[1]] = min(mn[to[1]], d[to[0]]);
		}
		for(auto to: g[v]) {
			if(cur.F + min(to[2] , sum[to[1]]-to[2]) < d[to[0]]) {
				st.erase({d[to[0]], to[0]});
				d[to[0]] = cur.F + min(to[2] , sum[to[1]]-to[2]);
				st.insert({d[to[0]], to[0]});
			}
			if(mn[to[1]] + sum[to[1]]-to[2] < d[to[0]]) {
				st.erase({d[to[0]], to[0]});
				d[to[0]] = mn[to[1]] + sum[to[1]]-to[2];
				st.insert({d[to[0]], to[0]});
			}
		}
		for(auto to: g[v]) {
			sum[to[1]] = 0;
			mn[to[1]] = INF;
		}
	}
	
	if(d[n] == INF) d[n] = -1;
	cout << d[n];
}
/*
*/
main() {
    ios;
    int tt = 1 , tc = 0;
    // cin >> tt;
    while(tt --) {
        solve(++tc);
    }
    return 0;
}

Compilation message (stderr)

Main.cpp:66:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   66 | main() {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...