//考试的时候把终点当成了n...
//如果需要在城市群 a 与城市群 b 之间连一条长度为 c 的无向边,则我们连长度为 c 的有向边 outa → inb 和 outb → ina.容易发现这样建出的图和原图中城市互相可达的情况是等价的
//
#include <bits/stdc++.h>
#define debug(x) std::cerr << #x" = " << (x) << std::endl
typedef long long LL;
const int XN = 20031, XNN = 4 * XN;
const LL INF = 0x3FFFFFFFFFFFFFFFLL;
std::vector<std::pair<int, int> > g[XNN];
int in[XN], out[XN];
int n = 0, m = 0, q = 0, s = 0, t = 0;
LL d[XNN];
typedef std::pair<LL, int> D;
void dij() {
std::fill(d, d + n + 1, INF);
d[s] = 0;
std::priority_queue<D, std::vector<D>, std::greater<D> > z;
z.push({0, s});
while (!z.empty()) {
D x = z.top();
z.pop();
int u = x.second;
if (u == t) return;
if (x.first != d[u]) continue;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].first;
if (d[u] + g[u][i].second < d[v]) z.push({d[v] = d[u] + g[u][i].second, v});
}
}
}
int main() {
//freopen("work.in", "r", stdin);
//freopen("work.out", "w", stdout);
scanf("%d%d%d", &n, &m, &q);
for (int i = 1, k = 0, x = 0; i <= m; i++) for (scanf("%d", &k), in[i] = ++n, out[i] = ++n; k--; ) scanf("%d", &x), g[x].push_back({out[i], 0}), g[in[i]].push_back({x, 0});
for (int tp = 0, u = 0, v = 0, w = 0; q--; ) scanf("%d%d%d%d", &tp, &u, &v, &w), tp == 1 ? (g[u].push_back({v, w}), g[v].push_back({u, w})) : (g[out[u]].push_back({in[v], w}), g[out[v]].push_back({in[u], w}));
scanf("%d%d", &s, &t);
dij();
printf("%lld\n", d[t] == INF ? -1 : d[t]);
return 0;
}
暑末 Day2 T1 Azuki has to work
最新推荐文章于 2024-11-04 19:37:15 发布