HDU 4360 as long as Binbin loves Sangsang
Description
Binbin misses Sangsang so much. He wants to meet with Sangsang as soon as possible.
Now Binbin downloads a maps from Elgoog. There is N (1<=n<=1,314) cities in the map and these cities is connected by M (0<=m<=13,520) Bi-direct roads. Each road have a length L (1<=l<=1,314,520) and is marked using a unique ID, which are a letter fromthe string "Love"!
Binbin rides a donkey, the donkey is so strange that it have to walk in the following sequence ' L ', ' O ', ' V '- > ' L ', ' O ' V ' E '-> ... etc.
Can you tell Binbin what far the donkey have to walk on order to meet with Sangsang?
Warning:sangsang would feel unhappy if binbin ride the donkey without a complete "love" string.
Binbin are at Node 1 and Sangsang are at node N.
Input
The first line has a integer T (1<=t<=520), indicate how many test cases bellow.
Each test case is begins with the integers n, M (n cities marked using a integer from 1 ... N and M roads).
Then following M lines, each of the line have four variables "U-V L letter", means that there was a road between City U,v (1<=u,v& lt;=n) with length L and the letter marked are ' L ', ' O ', ' V ' or ' E '
Output
For each test case, output a string
1. "Case: Binbin disappoint Sangsang again, damn it!"
If Binbin failed to meet with Sangsang or the donkey can ' t finish a path withthe full "Love" string.
2. "Case?: Cute Sangsang, Binbin would come with a donkey after travelling?" Meters and finding? Love strings at last. "
of cause, the travel distance should is as short as possible, and at the same time the ' love ' string should be as long as Possible.
Sample Input
2
4 4
1 2 1 L
2 1 1 O
1 3 1 V
3 4 1 E
4 4
1 2 1 L
2 3 1 O
3 4 1 V
4 1 1 E
Sample Output
Case 1:cute Sangsang, Binbin would come with a donkey after travelling 4 meters and finding 1 love strings at last.
Case 2:binbin disappoint Sangsang again, damn it!
BB to ride his little donkey to find SS, this is a very wonderful donkey, when it walks, will draw "love". Now give you n points, give you M road, each road includes the beginning of the end and the length and the donkey on this road will draw letters. Ask, in drawing the Complete "Love" premise, from the BB home point 1, to the SS home Point n the shortest circuit, as well as the full number of "loved" painted. "Love" at least to draw a complete one, or BB will be dumped. Problem-solving ideas: SPFA. Add four states "L", "O", "V", "E" to the original D array, and the judging condition is changed toD[u][letter] + Edges[i].dis < D[v][next__letter], and a two-dimensional CNT array is used to record the maximum number of letters drawn in the current state, whenD[u][letter] + Edges[i].dis = = D[v][next__letter]Time comparisonCnt[v][next__letter]And__cnt[u][letter] + 1, maintaining a CNT array. There is another place to pay attention to, if the SS in the BB home, this situation should pay attention to the special sentence. SS is in the BB home
1 4
1 1 1 L
1 1 1 O
1 1 1 V
1 1 1 E
Young people just like to do nothing.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intN =2400;Const intM =24000;Constll INF =1e15;intN, M, S, t;intVis[n];ll d[n][5];intEnintHEAD[M];structNode {intTo, next, let; ll Dis;} EDGE[M];voidAddedge (intUintV,ll x,intf) {edge[en].to = v; Edge[en].next = Head[u]; Edge[en].dis = x; Edge[en].let = f; Head[u] = en++; edge[en].to = u; Edge[en].next = Head[v]; Edge[en].dis = x; Edge[en].let = f; HEAD[V] = en++; }intcnt[n][4];voidSPFA () { Queue<int>Q; for(inti =1; I <= N; i++) { for(intj =0; J <=4; J + +) {D[i][j] = INF; CNT[I][J] =0; } Vis[i] =0; } d[s][0] =0; Vis[s] =1; Q.push (s); while(! Q.empty ()) {intU = Q.front (); Q.pop (); Vis[u] =0;intFlag =0; for(inti = Head[u]; I! =-1; i = edge[i].next) {intv = edge[i].to;intf = edge[i].let;intNext = (f +1) %4;if(D[u][f] + Edge[i].dis < D[v][next]) {D[v][next] = d[u][f] + Edge[i].dis; Cnt[v][next] = Cnt[u][f] +1;if(!vis[v]) {Q.push (v); VIS[V] =1; } }Else if(D[u][f] + Edge[i].dis = = D[v][next]) {if(Cnt[u][f] +1> Cnt[v][next]) {Cnt[v][next] = Cnt[u][f] +1; }}}}} ll check[5], sum;intInput () {sum =0;memset(Check,0,sizeof(check));intU, v; ll C;CharLintCNT =0; for(inti =0; I < m; i++) {scanf("%d%d%lld%c", &u, &v, &c, &l);intFlagif(L = =' L ') flag =0;Else if(L = =' O ') flag =1;Else if(L = =' V ') flag =2;Else if(L = =' E ') flag =3; Addedge (U, V, c, flag);if(U = =1&& v = =1&& n = =1) {if(!check[flag]) {cnt++; Check[flag] = c; }Else{Check[flag] = min (Check[flag], c); } } }returnCNT;}intMain () {intT, Case =1;scanf("%d", &t); while(t--) {en =0;memset(Head,-1,sizeof(head));printf("Case%d:", case++);scanf("%d%d", &n, &m); s =1, t = N;inttemp = input ();if(temp = =4) {sum = check[0] + check[1] + check[2] + check[3];printf("Cute Sangsang, Binbin would come with a donkey after travelling%lld meters and finding 1 love strings at last.\n" , sum);Continue; } SPFA ();if(! cnt[n][0] || d[n][0] = = INF) {printf("Binbin you disappoint Sangsang again, damn it!\n"); }Else{printf("Cute Sangsang, Binbin would come with a donkey after travelling%lld meters and finding%d love strings at last.\n" , d[n][0], cnt[n][0] /4); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4360 as long as Binbin loves Sangsang (Shortest way)