MUTC7 A-As long as Binbin loves Sangsang

As long as Binbin loves Sangsang

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


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
 
-----------

每条边除了有边权以外,还有一个字母标记。标记可以是“LOVE”里面任意字符。

每个点,要拆成四个点,分别代表到达该点的标记为L,O,V,E的最短路。

在只有一个节点的时候,有几条自环,至少必须走LOVE四条自环。此时,必须另外加一个节点表示开始节点。

---------

/** head-file **/

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <algorithm>

/** define-for **/

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)

/** define-useful **/

#define clr(x,a) memset(x,a,sizeof(x))
#define sz(x) int(x.size())
#define see(x) cerr<<#x<<" "<<x<<endl
#define se(x) cerr<<" "<<x
#define pb push_back
#define mp make_pair

/** test **/

#define Display(A, n, m) {                      \
    REP(i, n){                                  \
        REP(j, m) cout << A[i][j] << " ";       \
        cout << endl;                           \
    }                                           \
}

#define Display_1(A, n, m) {                    \
    REP_1(i, n){                                \
        REP_1(j, m) cout << A[i][j] << " ";     \
        cout << endl;                           \
    }                                           \
}

using namespace std;


/** typedef **/

typedef long long LL;

/** Add - On **/

const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long INFF = 1LL << 60;
const double EPS = 1e-9;
const double OO = 1e15;
const double PI = acos(-1.0); //M_PI;

const int maxn=2111;
const int maxm=63520;
int n,m;

struct EDGENODE{
    int to;
    LL w;
    int k;
    int next;
};
struct spnode{
    int u;
    LL len;
    LL stp;
    int k;
    spnode(int a=0,LL b=0,LL c=0,int d=0):u(a),len(b),stp(c),k(d){}
};

class CSPFA{
private:
    EDGENODE edges[maxm];
    int head[maxn],edge,node;
    queue<spnode>que;
public:
    LL dist[maxn][4];
    LL step[maxn][4];
    void addedge(int u,int v,LL c,int tp){
        edges[edge].w=c,edges[edge].k=tp,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        edges[edge].w=c,edges[edge].k=tp,edges[edge].to=u,edges[edge].next=head[v],head[v]=edge++;
    }
    void init(int n){
        memset(head,-1,sizeof(head));
        edge=0;
        node=n;
    }
    void SPFA(int src)
    {
        for (int i=0;i<=node;i++) REP(j,4) dist[i][j]=INFF;
        while (!que.empty()) que.pop();
        que.push(spnode(src,0,0,3));
        while (!que.empty()){
            spnode topnode=que.front();
            que.pop();
            int k=(topnode.k+1)%4;
            LL len=topnode.len;
            LL stp=topnode.stp;
            int top=topnode.u;
            for (int i=head[top];i!=-1;i=edges[i].next)
            {
                int v=edges[i].to;
                if (k!=edges[i].k) continue;
                if ( dist[v][k]>len+edges[i].w||
                    (dist[v][k]==len+edges[i].w&&step[v][k]<stp+1) )
                {
                    dist[v][k]=len+edges[i].w;
                    step[v][k]=stp+1;
                    que.push(spnode(v,dist[v][k],step[v][k],k));
                }
            }
        }
    }
}solver;

int apm(char c)
{
    if (c=='L') return 0;
    if (c=='O') return 1;
    if (c=='V') return 2;
    if (c=='E') return 3;
    return -1;
}

int main()
{
    int T,cas=0;
    cin>>T;
    while (T--)
    {
        cin>>n>>m;
        solver.init(n);
        while (m--)
        {
            int u,v,w;
            char c;
            cin>>u>>v>>w>>c;
            solver.addedge(u,v,w,apm(c));
        }
        solver.SPFA(1);
        if (solver.dist[n][3]==INFF)
            cout<<"Case "<<++cas<<": Binbin you disappoint Sangsang again, damn it!"<<endl;
        else
            cout<<"Case "<<++cas<<": Cute Sangsang, Binbin will come with a donkey after travelling "<<solver.dist[n][3]<<" meters and finding "<<solver.step[n][3]/4<<" LOVE strings at last."<<endl;
    }
    return 0;
}




posted on 2013-07-24 10:43  电子幼体  阅读(193)  评论(0编辑  收藏  举报

导航