Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define pb push_back
- #define llong long long
- #define ld long double
- #define endl '\n'
- #define pii pair<int, int>
- using namespace std;
- vector <pair<int, pii>> g[50000];
- map <pii, int> e;
- bool f[50000];
- set <int> my;
- set <int> buy;
- int r[2010];
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- srand(time(0));
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #endif // LOCAL
- int n, m;
- cin >> n >> m;
- for (int i = 0; i < n; ++i){
- cin >> r[i];
- }
- int sum = 0;
- for (int i = 0; i < m; ++i){
- int x, y, h, c;
- cin >> x >> y >> h >> c;
- --x; --y;
- g[x].pb({y, {c + r[y], i}});
- g[y].pb({x, {c + r[x], i}});
- e[{x, y}] = i;
- e[{y, x}] = i;
- if (h == 1){
- sum += c;
- my.insert(i);
- }
- else f[i] = 1;
- }
- //deikstra
- vector <int> dis(n, 1e9);
- vector <int> pr(n, 1e9);
- priority_queue<pii> q;
- q.push({0, 0});
- dis[0] = 0; pr[0] = 0;
- while (!q.empty()){
- int l = -q.top().F;
- int v = q.top().S;
- q.pop();
- if (l > dis[v]) continue;
- for (auto to : g[v]){
- int too = to.F;
- int len = to.S.F;
- if (dis[too] > dis[v] + len) {
- dis[too] = dis[v] + len;
- pr[too] = v;
- q.push({-dis[too], too});
- }
- }
- }
- if (dis[n - 1] > sum){
- cout << -1;
- return 0;
- }
- int i = n - 1;
- vector <int> path;
- vector <int> way;
- while (i != pr[i]){
- path.pb(e[{i, pr[i]}]);
- way.pb(i);
- i = pr[i];
- }
- way.pb(i);
- reverse(path.begin(), path.end());
- reverse(way.begin(), way.end());
- for (auto to : path){
- if (f[to] == 1){
- buy.insert(to);
- }
- else my.erase(to);
- }
- cout << my.size() << ' ';
- for (auto to : my){
- cout << to + 1 << ' ';
- }
- cout << endl;
- cout << buy.size() << ' ';
- for (auto to : buy){
- cout << to + 1 << ' ';
- }
- cout << endl;
- for (auto to : way){
- cout << to + 1 << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement