HDU - 4360As long as Binbin loves Sangsang最短路问题多状态记录

HDU - 4360
As long as Binbin loves Sangsang
Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Binbin misses Sangsang so much. He wants to meet with Sangsang as soon as possible. 
Now Binbin downloads a map from ELGOOG.There are N (1<=N<=1,314) cities in the map and these cities are connected by M(0<=M<=13,520) bi-direct roads. Each road has a length L (1<=L<=1,314,520)and is marked using a unique ID, which is a letter fromthe string “LOVE”! 
Binbin rides a DONKEY, the donkey is so strange that it has to walk in the following sequence ‘L’->’O’->’V’->’E’->’L’->’O’->’V’->’E’->.... etc. 
Can you tell Binbin how far the donkey has to walk in order to meet with Sangsang? 
WARNING: Sangsang will feel unhappy if Binbin ride the donkey without a complete”LOVE” string. 
Binbin is at node 1 and Sangsang is at node N.
 

Input

The first line has an integer T(1<=T<=520), indicate how many test cases bellow. 
Each test case begins with two integers N, M (N cities marked using an integer from 1…N and M roads). 
Then following M lines, each line has four variables“U V L letter”, means that there is a road between city U,V(1<=U,V<=N) with length L and the letter marked is‘L’,’O’,’V’ or ‘E’ 
 

Output

For each test case, output a string 
1.  “Case ?: Binbin you 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 will come with a donkey after travelling ? meters and finding ? LOVE strings at last.” 
Of cause, the travel distance should be 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 will come with a donkey after travelling 4 meters and finding 1 LOVE strings at last. Case 2: Binbin you disappoint Sangsang again, damn it!
/*
 *Author: 2486
 *Memory: 3376 KB		Time: 234 MS
 *Language: G++		Result: Accepted
 */

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 2500;
const int maxm = 25000;
const LL INF = 100000000000000000;
int T, N, M, U, V, L;
char OP[5];
LL d[maxn][4], v[maxn][4];
bool vis[maxn];


typedef struct edge {
    int to;
    LL val;
    int OP;
    edge(int to,LL val,int OP):to(to), val(val), OP(OP) {}
} P;


vector<edge> G[maxm];


void init() {
    for(int i = 1; i <= N; i ++) G[i].clear();
}


void Dijkstra(int s) {
    queue<int> que;
    d[s][0] = 0;
    vis[s] = true;
    que.push(s);
    while(!que.empty()) {
        int s = que.front();
        que.pop();
        vis[s] = false;//消除记忆,可以向回走,只要有路
        for(int i = 0; i < G[s].size(); i ++) {
            edge e = G[s][i];
            int next = (e.OP + 1) % 4;//取下一个
            if(d[e.to][next] > d[s][e.OP] + e.val) {//代表着选择了当前这条路
                d[e.to][next] = d[s][e.OP] + e.val;
                v[e.to][next] = v[s][e.OP] + 1;
                if(vis[e.to]) continue;
                que.push(e.to);
                vis[e.to] = true;//标记暂时不能向前走
            } else if(d[e.to][next] == d[s][e.OP] + e.val) {
                v[e.to][next] = max(v[e.to][next], v[s][e.OP] + 1);
            }
        }
    }
}


int main() {
    //freopen("D://imput.txt","r",stdin);
    int tt = 0;
    scanf("%d",&T);
    while(T --) {
        tt ++;
        scanf("%d%d", &N, &M);
        init();
        int sv;
        int F[5] = {0};
        int cnt = 0;
        for(int i = 1; i <= N; i ++) {
            for(int j = 0; j < 4; j ++) {
                d[i][j] = INF;
                v[i][j] = 0;
            }
            vis[i] = false;
        }
        
        
        for(int i = 1; i <= M; i ++) {
            scanf("%d%d%d%s", &U, &V, &L, OP);
            if(OP[0] == 'L')sv = 0;
            else if(OP[0] == 'O')sv = 1;
            else if(OP[0] == 'V')sv = 2;
            else if(OP[0] == 'E')sv = 3;
            G[U].push_back( P(V, L, sv));
            G[V].push_back( P(U, L, sv));
            if(U == 1 && V == 1 && N == 1) {//起点终点一样,要特殊处理
                if(!F[sv]) {
                    F[sv] = L;
                    cnt ++;
                } else {
                    F[sv] = min(F[sv], L);
                }
            }
        }
        
        
        printf("Case %d: ", tt);
        if(cnt == 4) {//如果正好能够组成"LOVE"则输出
            LL sum = F[0] + F[1] + F[2] + F[3];
            printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding 1 LOVE strings at last.\n", sum);
            continue;
        }
        Dijkstra(1);
        if(v[N][0] < 4 || d[N][0] == INF) {//不满足条件
            printf("Binbin you disappoint Sangsang again, damn it!\n");
        } else {
            printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %I64d LOVE strings at last.\n",d[N][0], v[N][0] / 4);
        }
    }
    return 0;
}


你可能感兴趣的:(HDU - 4360As long as Binbin loves Sangsang最短路问题多状态记录)