HDU4360: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): 2315    Accepted Submission(s): 544


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

 

=====================================题目大意=====================================


Binbin 想要去见Sangsang,所以他下载了一幅地图。

地图中共有N个城市(Binbin在城市1,Sangsang在城市N),城市之间有M条双向边,每条边都有一个长度L和一个字符标识(来自于

“LOVE”串)。

Binbin去见Sangsang的路径必须由至少一个的完整“LOVE"串组成,并且需要确保路径长度最短,此前提下还需确保走过的"LOVE”

串最多。


=====================================算法分析=====================================


Dijkstra水题,只是写起来麻烦了点。


=======================================代码=======================================




#include<queue>
#include<cstdio>
#include<cstring>

using namespace std;

typedef long long LLI;

const int INF1=0x7f;
const int MAXN=1320;
const int MAXM=13525;

int T,N,M,Head[MAXN],Tot,Cnt[MAXN][4];

LLI Dis[MAXN][4];                                //路径长度可能会超Int32

bool Vis[MAXN][4];

struct QNODE
{
    friend bool operator < (const QNODE& A,const QNODE& B) { return (A.dis>B.dis)||(A.dis==B.dis&&A.cnt<B.cnt); }
    QNODE(int Pt,int NexType,int Cnt,LLI Dis) { pt=Pt;  nextype=NexType;  cnt=Cnt;  dis=Dis; }
    int pt,nextype,cnt;                          //nextype为下一条边的类型
    LLI dis;
};

struct ENODE
{
    int pt,type;
    LLI dis;
    int next;
}Edge[MAXM<<1];

void Dijkstra()
{
    memset(Vis,0,sizeof(Vis));
    memset(Cnt,0,sizeof(Cnt));
    memset(Dis,INF1,sizeof(Dis));
    priority_queue<QNODE> q;
    q.push(QNODE(1,0,0,0));
    while(!q.empty())
    {
        QNODE cur=q.top();  q.pop();
        if(cur.pt==N&&!cur.nextype&&cur.cnt)     //因为至少要走一个“LOVE”串所以cur.cnt不能为0
        {
            printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters ",cur.dis);
            printf("and finding %d LOVE strings at last.\n",cur.cnt/4);
            return;
        }
        if(Vis[cur.pt][cur.nextype]) { continue; }
        Vis[cur.pt][cur.nextype]=true;
        for(int tmp=Head[cur.pt];tmp!=-1;tmp=Edge[tmp].next)
        {
            int pt=Edge[tmp].pt;
            int tp=Edge[tmp].type;
            LLI ds=Edge[tmp].dis;
            if((tp==cur.nextype)&&((ds<Dis[pt][tp]-cur.dis)||(ds==Dis[pt][tp]-cur.dis&&cur.cnt+1>Cnt[pt][tp])))
            {
                Dis[pt][tp]=cur.dis+ds;
                Cnt[pt][tp]=cur.cnt+1;
                q.push(QNODE(pt,(tp+1)%4,Cnt[pt][tp],Dis[pt][tp]));
            }
        }
    }
    printf("Binbin you disappoint Sangsang again, damn it!\n");
}

void AddEdge(int From,int To,int Len,int Type)
{
    Edge[Tot].pt=To;
    Edge[Tot].dis=Len;
    Edge[Tot].type=Type;
    Edge[Tot].next=Head[From];
    Head[From]=Tot++;
}

void ReaData()
{
    Tot=0;
    memset(Head,-1,sizeof(Head));
    memset(Edge,INF1,sizeof(Edge));;
    scanf("%d%d",&N,&M);
    for(int i=1;i<=M;++i)
    {
        int U,V,L,Type;
        char Letter;
        scanf("%d %d %d %c",&U,&V,&L,&Letter);
        if(Letter=='L') { Type=0; }
        else if(Letter=='O') { Type=1; }
        else if(Letter=='V') { Type=2; }
        else if(Letter=='E') { Type=3; }
        AddEdge(U,V,L,Type);
        AddEdge(V,U,L,Type);
    }
}

int main()
{
    while(scanf("%d",&T)==1)
    {
        for(int cas=1;cas<=T;++cas)
        {
            ReaData();
            printf("Case %d: ",cas);
            Dijkstra();
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值