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): 2779    Accepted Submission(s): 627

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!

题意描述:

n个点,m条边,起点为1,终点为n(注意!n可能等于1)的无向图(注意!无向图)。只能按照L->O->V->E->L->O->...的顺序走,求到达n点的最短长度和组成LOVE的最大个数。

解题思路:

SPFA,多一个dp数组记录,特判n=1的情况,路径长度可能超int。

参考代码:

#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const long long INF=1e17;
const int MAXN=1500;
typedef long long LL;
struct edge
{
    int to,love;
    LL value;
    edge(int a,LL b,int c)
    {
        to=a;
        value=b;
        love=c;
    }
};

vector<edge>G[MAXN];
int n,m,dp[MAXN][4];
LL mincost[MAXN][4];
bool used[MAXN];

void SPFA(int start)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<4;j++)
        {
            mincost[i][j]=INF;
            dp[i][j]=0;
        }
        used[i]=false;
    }
    queue<int>q;
    q.push(start);
    mincost[start][0]=0;
    used[start]=true;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        used[now]=false;
        for(int i=0;i<G[now].size();i++)
        {
            edge e=G[now][i];
            int next=(e.love+1)%4;
            if(mincost[e.to][next]>mincost[now][e.love]+e.value)
            {
                mincost[e.to][next]=mincost[now][e.love]+e.value;
                dp[e.to][next]=dp[now][e.love]+1;
                if(!used[e.to])
                {
                    q.push(e.to);
                    used[e.to]=true;
                }
            }
            else if(mincost[e.to][next]==mincost[now][e.love]+e.value)
                dp[e.to][next]=max(dp[e.to][next],dp[now][e.love]+1);
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int tcase,f=0;
    scanf("%d",&tcase);
    while(tcase--)
    {
        int spe=0;
        LL spec[4];
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            G[i].clear();
        memset(spec,0,sizeof(spec));
        for(int i=1;i<=m;i++)
        {
            int from,to,lov;
            LL val;
            char l[4];
            scanf("%d%d%lld%s",&from,&to,&val,l);
            if(l[0]=='L')
                lov=0;
            else if(l[0]=='O')
                lov=1;
            else if(l[0]=='V')
                lov=2;
            else if(l[0]=='E')
                lov=3;
            G[from].push_back(edge(to,val,lov));
            G[to].push_back(edge(from,val,lov));
            if(n==1)//特判只有一个点的情况
            {
                if(!spec[lov])
                {
                    spec[lov]=val;
                    spe++;
                }
                else
                    spec[lov]=min(spec[lov],val);
            }
        }
        printf("Case %d: ",++f);
        if(n==1)
        {
            if(spe==4)
            {
                LL sum=spec[0]+spec[1]+spec[2]+spec[3];
                printf("Cute Sangsang, Binbin will come with a donkey after travelling %lld meters and finding 1 LOVE strings at last.\n",sum);
            }
            else
                printf("Binbin you disappoint Sangsang again, damn it!\n");
            continue;
        }
        SPFA(1);
        if(mincost[n][0]==INF||dp[n][0]<4)//只需要计算完整的LOVE
            printf("Binbin you disappoint Sangsang again, damn it!\n");
        else
            printf("Cute Sangsang, Binbin will come with a donkey after travelling %lld meters and finding %d LOVE strings at last.\n",mincost[n][0],dp[n][0]/4);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-18 13:04:53

HDU 4360 As long as Binbin loves Sangsang(SPFA)的相关文章

hdu 4360 As long as Binbin loves Sangsang(最短路)

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 map from ELGOOG.There are N (1<=N<=1,314) cities in the map and these cities are connect

HDU 4360 As long as Binbin loves Sangsang spfa

题意: 给定n个点m条边的无向图 每次必须沿着LOVE走,到终点时必须是完整的LOVE,且至少走出一个LOVE, 问这样情况下最短路是多少,在一样短情况下最多的LOVE个数是多少. 有自环. #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <iostream> #include <algorithm> using names

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

As long as Binbin loves Sangsang

题目连接 题意: 给定一个无向图,每个边有两个属性,长度和一个字母'L','O','V','E'中的一个.从1点开始到达n点,每次必须按照L -> O -> V -> E -> ... -> E的顺序,到达终点时候必须经过E边 分析: 对于这种对边的限制,比较简单的方法是将一个点拆成若干个点.因为经过'L'到达点p的状态和经过'O'到达点p的状态时不一样的,第一个之后只能经过'O'边,而第二个只能经过'V'边,所以经过不同的边到达同一个点的时候对应的状态应该分开,也就是将点拆

hdu 4882 ZCC Loves Codefires(贪心)

# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; struct node { int v; int t; }; struct node a[100010]; bool cmp(node a,node b) { return a.v *a.t+(a.v+b.v)*b.t<b.v*b.t+(a.v+b.v)*a.t; } int main() { int

HDU 4512 吉哥系列故事——完美队形(LCIS)

Problem Description 吉哥这几天对队形比较感兴趣. 有一天,有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求,则称之为完美队形: 1.挑出的人保持他们在原队形的相对顺序不变: 2.左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2个人和第m-1个人身高相同,依此类推,当然,如果m是奇数,中间那个人可以任意: 3.从左到中间那个人,身高需保证递增,如

hdu4882ZCC Loves Codefires(贪心)

题目链接: 啊哈哈,选我选我 题目: ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 182    Accepted Submission(s): 97 Problem Description Though ZCC has many Fans, ZCC himself is a crazy Fan

hdu4882-ZCC Loves Codefires(贪心)

题目:hdu4882-ZCC Loves Codefires 题目大意:给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道problem需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最少. 解题思路: e1 e2 k1  1   2 k2 3    4 这样的两道问题的组合方式有两种:12组合 费用: 1 * 3 + (1 + 2) * 4 = 1 * 3 + 2 *4 + 1 * 4 21组合 费用: 2 * 4 +

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we ca