Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /// from HUNG MINE with love <3
- long long d[100001];
- int n, m1, m2, inq[100001], s, t, din[100001], dout[100001];
- typedef pair <int, int> ii;
- vector <ii> a[100001];
- void bell () {
- for (int i = 1; i <= n; ++ i) {
- d[i] = 4e18;
- }
- d[t] = 0;
- queue <int> q;
- q.push (t);
- inq[t] = 1;
- while (q.size ()) {
- int u = q.front ();
- inq[u] = 0;
- q.pop ();
- for (int i = 0; i < a[u].size (); ++ i) {
- int v = a[u][i].first;
- long long uv = a[u][i].second;
- if (d[v] > d[u] + uv) {
- d[v] = d[u] + uv;
- if ( !inq[v]) {
- q.push (v);
- inq[v] = 1;
- }
- }
- }
- }
- }
- int main () {
- if (fopen ("test.inp", "r")) {
- freopen ("test.inp", "r", stdin);
- }
- // else {
- // freopen ("EDGES.inp", "r", stdin);
- // freopen ("EDGES.out", "w", stdout);
- // }
- ios_base :: sync_with_stdio (0);
- cin.tie (0);
- cout.tie (0);
- cin >> n;
- cin >> m1;
- while (m1 --) {
- int x, y;
- cin >> x >> y;
- a[x].push_back (ii (y, 0));
- ++ dout[x];
- ++ din[y];
- }
- cin >> m2;
- while (m2 --) {
- int x, y, z;
- cin >> x >> y >> z;
- a[x].push_back (ii (y, z));
- }
- for (int i = 1; i <= n; ++ i) {
- if (dout[i] == 0) {
- t = i;
- }
- if (din[i] == 0) {
- s = i;
- }
- }
- bell ();
- if (d[s] == 4e18) {
- cout << "NO";
- }
- else {
- cout << "YES\n" << d[s];
- }
- }
Add Comment
Please, Sign In to add comment