Question connection
- Question:
Given an undirected graph, each side has two attributes: length and one of the letters 'l', 'O', 'V', and 'E. Starting from to, each time it must be in the order of L-> O-> V-> E->...-> E. When it reaches the end point, it must go through the E side.
- Analysis:
A simple method for this edge restriction is to split a vertex into several vertices. Because the status of arriving at point P after 'L' is different from that of arriving at point P after 'O', the first one can only go through the 'O' side, however, the second vertex can only pass through the 'V' edge. Therefore, when different edges reach the same vertex, the corresponding state should be separated, that is, the vertex should be split into four vertices, it indicates that the pvertex is reached by four sides.
- Note:
A graph can have a self-ring or only one vertex.
The path must have at least one love
The path length should be as small as possible. If the length is equal, the number of love words should be as large as possible.
Dijkstra method: (this method can be used to determine a specific point to avoid unnecessary troubles)
Const ll INF = 1e18; const int maxv = 10000; struct edge {ll from, to, DIST;}; struct heapnode {ll D, U, num; bool operator <(const heapnode & RHs) const {return D> RHS. d ;}}; struct Dijkstra {int N; // n: point M: Temporary Variable Vector <edge> edges; // store all edge vectors <int> G [maxv]; // All adjacent edge numbers of each vertex bool done [maxv]; // whether the ll d [maxv] is permanently labeled; // The distance from the start point to each vertex of LL num [maxv]; void Init (int n) {This-> N = N; For (INT I = 0; I <n; I ++) G [I]. clear (); edges. clear ();} void addedge (int from, int to, int Dist) {G [from]. push_back (edges. size (); edges. push_back (edge) {from, to, DIST});} void Dijkstra (INT s) {priority_queue
Spfa method: (a single point is also a special sentence, and the method of adding points is the same as that of Dijkstra)
const LL INF = 1e18;const int MAXV = 10000;struct Edge{ int from, to, dist;};struct SPFA{ int n; LL d[MAXV]; int num[MAXV]; vector<Edge> edges; vector<int> G[MAXV]; bool inq[MAXV]; void init(int n) { this->n = n; edges.clear(); REP(i, n) G[i].clear(); } void AddEdge(int from, int to, int dist) { G[from].push_back(edges.size()); edges.push_back((Edge) {from, to, dist}); } void spfa(int s) { queue<int> q; CLR(inq, false); CLR(num, 0); REP(i, n) d[i] = INF; d[s] = 0; q.push(s); inq[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; REP(i, G[u].size()) { Edge& e = edges[G[u][i]]; if (d[e.to] == d[u] + e.dist && num[u] + 1 > num[e.to]) { num[e.to] = num[u] + 1; if (!inq[e.to]) { q.push(e.to); inq[e.to] = true; } } if(d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; num[e.to] = num[u] + 1; if (!inq[e.to]) { q.push(e.to); inq[e.to] = true; } } } } }} spfa;LL chk[4];int main(){ int T; RI(T); FE(kase, 1, T) { REP(i, 4) chk[i] = INF; int n, m, u, v, d, op; char type; RII(n, m); spfa.init(n << 2); REP(i, m) { scanf("%d%d%d %c", &u, &v, &d, &type); u--; v--; if (type == 'L') op = 0; else if (type == 'O') op = 1; else if (type == 'V') op = 2; else op = 3; chk[op] = min(chk[op], (LL)d); spfa.AddEdge(u + (op + 3) % 4 * n, v + op * n, d); spfa.AddEdge(v + (op + 3) % 4 * n, u + op * n, d); } printf("Case %d: ", kase); if (n == 1) { REP(i, 4) if (chk[i] == INF) { puts("Binbin you disappoint Sangsang again, damn it!"); goto end; } printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n" , chk[0] + chk[1] + chk[2] + chk[3], 1); end:; } else { spfa.spfa(3 * n); if (spfa.d[4 * n - 1] == INF) puts("Binbin you disappoint Sangsang again, damn it!"); else { printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n" , spfa.d[4 * n - 1], spfa.num[4 * n - 1] / 4); } } } return 0;}
By the way, you can get some test data for troubleshooting.
4
1 2 1 L
2 1 1 o
1 3 1 V
3 4 1 E
Ans: 4, 1
4
1 2 1 L
2 3 1 o
3 4 1 V
4 1 1 E
Ans: No
12 12
1 5 10 L
5 6 10 o
6 7 10 V
7 12 10 E
1 2 1 L
2 3 1 o
3 4 1 V
4 8 1 E
8 9 1 L
9 10 1 o
10 11 1 V
11 12 33 E
Ans: 40, 2
12 12
1 5 10 L
5 6 10 o
6 7 10 V
7 12 10 E
1 2 1 L
2 3 1 o
3 4 1 V
4 8 1 E
8 9 1 L
9 10 1 o
10 11 1 V
11 12 34 E
Ans: 40, 1
1 4
1 1 1 L
1 1 1 o
1 1 1 V
1 1 1 E
Ans: 4, 1
2 8
1 1 2 L
1 1 1 o
1 1 1 V
1 1 1 E
1 2 3 L
2 1 1 o
1 2 1 V
2 1 1 E
Ans: 5, 1
1 3
1 1 1 L
1 1 1 o
1 1 1 E
Ans: No
11
1 2 1 L
2 3 1 o
3 4 348 v
1000 E
1 5 50 L
5 6 50 O
6 7 50 V
7 8 50 e
8 9 50 L
9 10 50 O
10 4 50 V
Ans: 1350 2