HDU 4360 As long as Binbin loves Sangsang (SPFA)

As long as Binbin loves Sangsang

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1601    Accepted Submission(s): 429


Problem 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
FZU
 
Source
 
Recommend
zhuyuanchen520
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>

using namespace std;

const int VM=1500;
const int EM=15000;
const int INF=999999999;

map<char,int> mp;
int n,m,cnt;
int head[VM],vis[VM][4];
long long dis[VM][4];

struct Edge{
    int to,nxt,id;
    int cap;
}edge[EM<<1];

struct data{
    int u;
    long long cap;
    int stp,id;
    data(int _u,long long _cap,int _stp,int _id):u(_u),cap(_cap),stp(_stp),id(_id){}
};

void addedge(int cu,int cv,int cw,int id){
    edge[cnt].to=cv;
    edge[cnt].cap=cw;
    edge[cnt].id=id;
    edge[cnt].nxt=head[cu];
    head[cu]=cnt++;
}

void SPFA(){
    queue<data> q;
    while(!q.empty())
        q.pop();
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    q.push(data(1,0,0,3));
    while(!q.empty()){
        data cur=q.front();
        q.pop();
        int u=cur.u;
        int id=(cur.id+1)%4;
        long long cap=cur.cap;
        int stp=cur.stp;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(id==edge[i].id){
                if(dis[v][id]>cap+edge[i].cap || (dis[v][id]==cap+edge[i].cap && vis[v][id]<stp+1)){
                    dis[v][id]=cap+edge[i].cap;
                    vis[v][id]=stp+1;
                    q.push(data(v,dis[v][id],vis[v][id],id));
                }
            }
        }
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    mp['L']=0; mp['O']=1; mp['V']=2; mp['E']=3;
    int t,cases=0;
    scanf("%d",&t);
    while(t--){
        cnt=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        int u,v,w;
        char str[3];
        while(m--){
            scanf("%d %d %d %s",&u,&v,&w,str);
            addedge(u,v,w,mp[str[0]]);
            addedge(v,u,w,mp[str[0]]);
        }
        SPFA();
        if(dis[n][3]==dis[n+1][0])
            printf("Case %d: Binbin you disappoint Sangsang again, damn it!\n",++cases);
        else
            printf("Case %d: Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",++cases,dis[n][3],vis[n][3]/4);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jackge/p/3069057.html