QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#687868#2178. Robotliuziao0 944ms207464kbC++234.8kb2024-10-29 21:39:402024-10-29 21:39:40

Judging History

你现在查看的是最新测评结果

  • [2024-10-29 21:39:40]
  • 评测
  • 测评结果:0
  • 用时:944ms
  • 内存:207464kb
  • [2024-10-29 21:39:40]
  • 提交

answer

#include <bits/stdc++.h>

// #define int int64_t

const int kMaxN = 1e5 + 5, kMaxM = 2e5 + 5;

int n, m, cnt;
int u[kMaxM], v[kMaxM], c[kMaxM], w[kMaxM], idx[kMaxN];
std::unordered_map<int, int> id[kMaxN];
std::unordered_map<int, int64_t> val[kMaxN];
std::vector<std::tuple<int, int, int, int64_t>> G[kMaxM * 5][2][2]; // [to, o1, o2, w]

// int cost(int a, int o1, int b, int o2) {
//   if (o2) return w[b];
//   int u, ret;
//   if (::u[a] == ::u[b] || ::u[a] == ::v[b]) u = ::u[a];
//   else u = ::v[a];
//   if (o1 && c[a] == c[b]) return mp[u][c[b]] - w[a] - w[b];
//   else return mp[u][c[b]] - w[b];
// }

void dijkstra() {
  static int dis[kMaxM][2][2];
  std::priority_queue<std::tuple<int, int, int, int>> q;
  memset(dis, 0x3f, sizeof(dis));
  for (int i = 1; i <= m; ++i) {
    if (u[i] == 1) {
      dis[i][0][0] = val[1][c[i]] - w[i];
      dis[i][0][1] = w[i];
      q.emplace(-dis[i][0][0], i, 0, 0);
      q.emplace(-dis[i][0][1], i, 0, 1);
    } else if (v[i] == 1) {
      dis[i][1][0] = val[1][c[i]] - w[i];
      dis[i][1][1] = w[i];
      q.emplace(-dis[i][1][0], i, 0, 0);
      q.emplace(-dis[i][1][1], i, 0, 1);
    }
  }
  for (; !q.empty();) {
    auto [d, i, o1, o2] = q.top();
    q.pop();
    // std::cerr << d << ' ' << i << ' ' << o1 << ' ' << o2 << '\n';
    // for (int j = 1; j <= m; ++j) {
    //   if (i == j) continue;
    //   for (int o3 = 0; o3 < 2; ++o3) {
    //     for (int o4 = 0; o4 < 2; ++o4) {
    //       if ((o1 ? u[i] : v[i]) == (!o3 ? u[j] : v[j])) {
    //         int val = dis[i][o1][o2] + cost(i, o2, j, o4);
    //         if (dis[j][o3][o4] > val) {
    //           dis[j][o3][o4] = val;
    //           q.emplace(-dis[j][o3][o4], j, o3, o4);
    //         }
    //       }
    //     }
    //   }
    // }
  }
  int ret = 1e18;
  for (int i = 1; i <= m; ++i) {
    for (int o1 = 0; o1 < 2; ++o1) {
      for (int o2 = 0; o2 < 2; ++o2) {
        if ((o1 ? u[i] : v[i]) == n) ret = std::min(ret, dis[i][o1][o2]);
      }
    }
  }
  if (ret <= 1e15) std::cout << ret << '\n';
  else std::cout << "-1\n";
}

void spfa() {
  static int dis[kMaxM * 5][2][2];
  static bool inq[kMaxM * 5][2][2];
  memset(dis, 0x3f, sizeof(dis));
  std::queue<std::tuple<int, int, int>> q;
  for (int i = 1; i <= m; ++i) {
    if (u[i] == 1) {
      dis[i][0][0] = val[1][c[i]] - w[i];
      dis[i][0][1] = w[i];
      q.emplace(i, 0, 0);
      q.emplace(i, 0, 1);
      inq[i][0][0] = inq[i][0][1] = 1;
    } else if (v[i] == 1) {
      dis[i][1][0] = val[1][c[i]] - w[i];
      dis[i][1][1] = w[i];
      q.emplace(i, 1, 0);
      q.emplace(i, 1, 1);
      inq[i][1][0] = inq[i][1][1] = 1;
    }
  }
  for (; !q.empty();) {
    auto [u, o1, o2] = q.front(); q.pop();
    inq[u][o1][o2] = 0;
    for (auto [v, o3, o4, w] : G[u][o1][o2]) {
      if (dis[v][o3][o4] > dis[u][o1][o2] + w) {
        dis[v][o3][o4] = dis[u][o1][o2] + w;
        if (!inq[v][o3][o4]) {
          q.emplace(v, o3, o4);
          inq[v][o3][o4] = 0;
        }
      }
    }
  }
  int ret = 1e18;
  for (int i = 1; i <= m; ++i) {
    for (int o1 = 0; o1 < 2; ++o1) {
      for (int o2 = 0; o2 < 2; ++o2) {
        if ((o1 ? u[i] : v[i]) == n) ret = std::min(ret, dis[i][o1][o2]);
      }
    }
  }
  if (ret <= 1e15) std::cout << (int)ret << '\n';
  else std::cout << "-1\n";
}

void dickdreamer() {
  std::cin >> n >> m;
  cnt = m;
  for (int i = 1; i <= n; ++i) idx[i] = ++cnt;
  for (int i = 1; i <= m; ++i) {
    std::cin >> u[i] >> v[i] >> c[i] >> w[i];
    val[u[i]][c[i]] += w[i], val[v[i]][c[i]] += w[i];
    if (!id[u[i]].count(c[i])) id[u[i]][c[i]] = ++cnt;
    if (!id[v[i]].count(c[i])) id[v[i]][c[i]] = ++cnt;
  }
  for (int i = 1; i <= m; ++i) {
    G[i][0][0].emplace_back(idx[v[i]], 0, 0, 0);
    G[i][0][1].emplace_back(idx[v[i]], 0, 0, 0);
    G[i][0][1].emplace_back(id[v[i]][c[i]], 0, 0, -w[i]);

    G[i][1][0].emplace_back(idx[u[i]], 0, 0, 0);
    G[i][1][1].emplace_back(idx[u[i]], 0, 0, 0);
    G[i][1][1].emplace_back(id[u[i]][c[i]], 0, 0, -w[i]);

    G[idx[u[i]]][0][0].emplace_back(i, 0, 0, val[u[i]][c[i]] - w[i]);
    G[idx[u[i]]][0][0].emplace_back(i, 0, 1, w[i]);

    G[idx[v[i]]][0][0].emplace_back(i, 1, 0, val[v[i]][c[i]] - w[i]);
    G[idx[v[i]]][0][0].emplace_back(i, 1, 1, w[i]);

    G[id[u[i]][c[i]]][0][0].emplace_back(i, 0, 0, val[u[i]][c[i]] - w[i]);
    G[id[v[i]][c[i]]][0][0].emplace_back(i, 1, 0, val[v[i]][c[i]] - w[i]);
  }
  // dijkstra();
  spfa();
}

int32_t main() {
#ifdef ORZXKR
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
  int T = 1;
  // std::cin >> T;
  while (T--) dickdreamer();
  // std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 34
Accepted
time: 6ms
memory: 38328kb

input:

2 1
1 2 1 10

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Wrong Answer
time: 3ms
memory: 36452kb

input:

8 1
5 6 1 7

output:

2147483647

result:

wrong answer 1st lines differ - expected: '-1', found: '2147483647'

Subtask #2:

score: 0
Wrong Answer

Test #21:

score: 24
Accepted
time: 179ms
memory: 96096kb

input:

25437 78923
921 9998 30945 1
5452 13736 24464 1
11746 24238 24464 1
10875 12958 24464 1
12267 20617 30945 1
3738 16549 35589 1
16223 16940 35589 1
1303 23059 24464 1
12424 21853 24464 1
11198 20674 35589 1
15645 19099 30945 1
8860 9441 24464 1
3609 15160 35589 1
22638 23472 24464 1
766 8991 35589 1
...

output:

5

result:

ok single line: '5'

Test #22:

score: 24
Accepted
time: 75ms
memory: 65984kb

input:

15761 34565
6553 7857 4268 1
1139 8149 4268 1
4136 9004 4268 1
3810 8194 27009 1
3005 10547 9061 1
3025 15018 27009 1
5735 6627 9061 1
2337 12263 9061 1
4260 8046 9061 1
12549 14043 4268 1
6992 11456 4268 1
833 4225 4268 1
1609 9603 4268 1
2588 9564 9061 1
2361 8162 27009 1
10250 10706 4268 1
6878 1...

output:

6

result:

ok single line: '6'

Test #23:

score: 24
Accepted
time: 242ms
memory: 118136kb

input:

16917 133722
10190 12792 75746 1
4125 15443 75746 1
163 5884 29061 1
2756 10488 29061 1
7080 9199 75746 1
1463 4211 75746 1
5115 7366 29061 1
10308 11662 75746 1
1196 14025 29061 1
2863 14178 75746 1
9795 13347 75746 1
13003 13888 75746 1
5035 16673 75746 1
1356 15130 75746 1
13068 16408 29061 1
349...

output:

3

result:

ok single line: '3'

Test #24:

score: 24
Accepted
time: 119ms
memory: 77100kb

input:

25063 52020
145 18915 3816 1
5378 20731 3816 1
17344 23239 21514 1
2212 6628 3816 1
3462 15824 3816 1
1660 15356 21514 1
7250 18036 21514 1
8600 17595 21514 1
1446 12372 3816 1
10292 22860 3816 1
6562 18473 21514 1
10903 20797 21514 1
5932 8895 21514 1
5310 8230 3816 1
15875 24245 21514 1
3521 3908 ...

output:

5

result:

ok single line: '5'

Test #25:

score: 24
Accepted
time: 531ms
memory: 207464kb

input:

99999 199988
4731 83366 89897 1
80907 83366 155057 1
1 15876 82022 1
83366 86520 193680 1
15678 83366 180045 1
31911 59651 263 1
1 61854 122596 1
83366 99983 63626 1
67027 83366 28130 1
19313 83366 74260 1
59651 87942 97680 1
1 98039 115849 1
53600 83366 150525 1
26771 83366 120178 1
80859 83366 537...

output:

3

result:

ok single line: '3'

Test #26:

score: 24
Accepted
time: 491ms
memory: 201244kb

input:

99999 199988
11815 64227 56577 1
11815 60572 63128 1
1 78695 24435 1
12018 22361 50088 1
11815 67504 38323 1
12018 19012 107473 1
11815 63558 63128 1
71961 74024 63128 1
11815 63837 63128 1
11815 35595 63128 1
12018 57914 87572 1
12018 54264 88988 1
12018 66879 117942 1
1 49113 107309 1
12018 81198 ...

output:

3

result:

ok single line: '3'

Test #27:

score: 24
Accepted
time: 382ms
memory: 182312kb

input:

100000 199990
37990 50087 155359 1
22611 37990 155359 1
4362 89790 155359 1
4362 96346 155359 1
4362 54455 155359 1
37990 93050 155359 1
31313 37990 155359 1
36462 37990 155359 1
4362 14580 155359 1
1140 4362 155359 1
4362 70772 155359 1
4362 53585 155359 1
37990 49713 155359 1
4362 47065 155359 1
3...

output:

3

result:

ok single line: '3'

Test #28:

score: 24
Accepted
time: 944ms
memory: 178732kb

input:

100000 199839
11865 45830 161515 1
46988 77944 161515 1
37628 62851 161515 1
21213 29495 161515 1
79467 91266 161515 1
42910 57123 161515 1
83278 89082 161515 1
17867 67021 161515 1
29361 92753 161515 1
32817 45699 161515 1
90648 93260 161515 1
14450 54441 161515 1
27147 85147 161515 1
11506 24762 1...

output:

12347

result:

ok single line: '12347'

Test #29:

score: 24
Accepted
time: 583ms
memory: 178764kb

input:

100000 200000
19447 99453 101188 1
4820 52736 101188 1
51887 94783 101188 1
20762 99520 101188 1
4745 89605 101188 1
49080 66338 101188 1
24656 26963 101188 1
64986 82044 101188 1
22890 89840 101188 1
483 35061 101188 1
72709 85911 101188 1
7597 76399 101188 1
17356 66290 101188 1
73758 94642 101188...

output:

1250

result:

ok single line: '1250'

Test #30:

score: 0
Wrong Answer
time: 362ms
memory: 133684kb

input:

53901 119501
10291 14784 53288 1
4583 13856 53288 1
23363 33647 101468 1
3518 53845 101468 1
36419 38250 112685 1
3198 19748 112685 1
26033 32687 112685 1
14393 44922 112685 1
25180 38428 53288 1
23010 26128 112685 1
2403 51848 101468 1
12179 20699 101468 1
1420 27014 53288 1
35027 39700 112685 1
20...

output:

1061109567

result:

wrong answer 1st lines differ - expected: '-1', found: '1061109567'

Subtask #3:

score: 0
Skipped

Dependency #1:

0%