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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值