HDU-4360 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): 4094    Accepted Submission(s): 880


 

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

2012 Multi-University Training Contest 7

 

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  2426 3790 3435 3917 4309 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 2500;
const int MAXM = 25000;
#define INF 0x3f3f3f3f3f3f3f3f     //long long 就定义更大的
long long dis[MAXN][4], cnt[MAXN][4];
bool vis[MAXN];
queue<int>q;
struct Node
{
    int to;
    long long w;
    int letter;
};
vector<Node> G[MAXM];
void addedge(int u,int v,long long w,int letter)
{

    Node t={v,w,letter};
    G[u].push_back(t);
}
void init(int n)
 {
    for(int i = 1; i <= n; i ++) G[i].clear();
}
void spfa(int s,int n)//单源最短路(s为起点,n为节点总数)
{
    int u;
    for(int i=0;i<=n;i++)
    for(int j=0;j<4;j++)
    {
         dis[i][j]=INF;
         cnt[i][j]=0;
    }
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    q.pop();
    q.push(s);
    vis[s]=true;
    dis[s][0]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=0;i<G[u].size();i++)
        {
            int v=G[u][i].to;
            long long w=G[u][i].w;
            int letter=G[u][i].letter;
            int next_letter=(letter+1)%4;
            if(dis[v][next_letter]>dis[u][letter]+w)  //原来的一个点变成四个点
            {                                         //同样是唯一确定的之前状态
                dis[v][next_letter]=dis[u][letter]+w;
                cnt[v][next_letter]=cnt[u][letter]+1;    //累积一定是按LOVE顺序累积个数的,所以cnt[u][0]/4就能表示几个LOVE
                if(!vis[v])                              //cnt是从0开始的,所以9个数,只是8
                {                                        //cnt[u][0]前面必须是E才有值,不然就是0.
                    vis[v]=true;
                    q.push(v);
                }
            }
            else if(dis[v][next_letter]==dis[u][letter]+w)
            {
                cnt[v][next_letter]=max(cnt[v][next_letter],cnt[u][letter]+1);
            }
        }
    }
}
int main()
{
    int T;
    int n,m;
    int u,v;
    long long w;
    int Case = 1;
    char str;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        init(n);
        int letter;
        long long F[5] = {0};
        int Count = 0;
        for(int i = 1; i <= n; i ++)
        {
            for(int j = 0; j < 4; j ++)
            {
                dis[i][j] = INF;
                cnt[i][j] = 0;
            }
            vis[i] = false;
        }
        for(int i = 1; i <= m; i ++)
        {
            scanf("%d %d %lld %c", &u, &v, &w,&str);
            if(str == 'L')
			letter=0;
            else if(str == 'O')
			letter=1;
            else if(str == 'V')
			letter=2;
            else if(str == 'E')
			letter=3;
            addedge(u,v,w,letter);
            addedge(v,u,w,letter);
            if(u== 1 && v == 1 && n == 1) {//起点终点一样,要特殊处理
                if(!F[letter])
				{
                    F[letter] = w;
                    Count++;
                } else
			    {
                    F[letter] = min(F[letter], w);
                }
            }
        }
        printf("Case %d: ", Case++);
        if(Count == 4)
	    {//如果正好能够组成"LOVE"则输出
            long long 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;
        }
        spfa(1,n);
        if(cnt[n][0]==0||dis[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",dis[n][0], cnt[n][0]/4);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/81784869