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

题目大意:BB要骑着他的小毛驴去找SS,这是一只很奇葩的驴,它走路的时候,会画出“LOVE”。现在给你n个点,给你m条路,每条路包括起点终点以及长度和驴在这条路上会画出的字母。问,在画出完整的“LOVE”的前提下,从BB家点1,到SS家点n的最短路,以及画出的完整的“LOVE”的个数。“LOVE”至少要画完整的一个,否则BB就要被甩了。

解题思路:SPFA。在原先的d数组上加四个状态“L”,“O”,“V”,“E“,判断条件改为 d[u][letter] + edges[i].dis < d[v][next__letter],并用一个二维cnt数组记录当前状态所画的最多的字母数,当d[u][letter] + edges[i].dis == d[v][next__letter]时比较cnt[v][next__letter]和__cnt[u][letter] + 1,维护cnt数组。还有一个要注意的地方,如果SS就在BB家,这种情况要注意特判。

SS就在BB家

1 4

1 1 1 L

1 1 1 O

1 1 1 V

1 1 1 E

年轻人就是喜欢没事找事
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;

typedef long long ll;
const int N = 2400;
const int M = 24000;
const ll INF = 1e15;
int n, m, s, t;
int vis[N];
ll d[N][5];

int en;
int head[M];

struct node {
    int to, next, let;
    ll dis;
}edge[M];  

void addEdge(int u,int v,ll x, int f) {
    edge[en].to = v;
    edge[en].next = head[u];
    edge[en].dis = x;
    edge[en].let = f;
    head[u] = en++;  

    edge[en].to = u;
    edge[en].next = head[v];
    edge[en].dis = x;
    edge[en].let = f;
    head[v] = en++;
}  

int Cnt[N][4];
void SPFA() {
    queue<int> Q;
    for(int i = 1; i <= n; i++) {
        for (int j = 0; j <= 4; j++) {
            d[i][j] = INF;
            Cnt[i][j] = 0;
        }
        vis[i] = 0;
    }
    d[s][0] = 0;
    vis[s] = 1;
    Q.push(s);
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        int flag = 0;
        for(int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].to;
            int f = edge[i].let;
            int next = (f + 1) % 4;
            if(d[u][f] + edge[i].dis < d[v][next]) {
                d[v][next] = d[u][f] + edge[i].dis;
                Cnt[v][next] = Cnt[u][f] + 1;
                if(!vis[v]) {
                    Q.push(v);
                    vis[v] = 1;
                }
            } else if (d[u][f] + edge[i].dis == d[v][next]) {
                if (Cnt[u][f] + 1 > Cnt[v][next]) {
                    Cnt[v][next] = Cnt[u][f] + 1;
                }
            }
        }
    }
} 

ll check[5], sum;
int input() {
    sum = 0;
    memset(check, 0, sizeof(check));
    int u, v;
    ll c;
    char l;
    int cnt = 0;
    for (int i = 0; i < m; i++) {
        scanf("%d %d %lld %c", &u, &v, &c, &l);
        int flag;
        if (l == ‘L‘) flag = 0;
        else if (l == ‘O‘) flag = 1;
        else if (l == ‘V‘) flag = 2;
        else if (l == ‘E‘) flag = 3;
        addEdge(u, v, c, flag);
        if (u == 1 && v == 1 && n == 1) {
            if (!check[flag]) {
                cnt++;
                check[flag] = c;
            } else {
                check[flag] = min(check[flag], c);
            }
        }
    }
    return cnt;
}

int main() {
    int T, Case = 1;
    scanf("%d", &T);
    while (T--) {
        en = 0;
        memset(head, -1, sizeof(head));
        printf("Case %d: ", Case++);
        scanf("%d %d", &n, &m);
        s = 1, t = n;
        int temp = input();
        if (temp == 4) {
            sum = check[0] + check[1] + check[2] + check[3];
            printf("Cute Sangsang, Binbin will come with a donkey after travelling %lld meters and finding 1 LOVE strings at last.\n", sum);
            continue;
        }
        SPFA();
        if (!Cnt[n][0] || d[n][0] == INF) {
            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", d[n][0], Cnt[n][0] / 4);
        }
    }
    return 0;
}

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

时间: 2024-08-04 08:52:31

hdu 4360 As long as Binbin loves Sangsang(最短路)的相关文章

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 Sa

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'边,所以经过不同的边到达同一个点的时候对应的状态应该分开,也就是将点拆

hdu4360 As long as Binbin loves Sangsang spfa变形

题意:给定n个点m条边的无向图,每次必须沿着LOVE走,到终点时必须是完整的LOVE,且至少走出一个LOVE,问这样情况下最短 路是多少,在一样短情况下最多的LOVE个数是多少.注意:有自环!(见底下的数据) 思路:其实本质就是个最短路,用spfa就好.注意自环的特殊处理,详见代码: /********************************************************* file name: hdu4360.cpp author : kereo create tim

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

题目地址:HDU 1839 我去..原来这题这么简单...网络流中这种二分建图的方式做了一大堆了..这种题还能难倒我吗...白天一直没怎么看懂题,对题意懵懵懂懂的...晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗....只是把网络流算法改成最短路就行了..但是两个地方手残了没能在实验室当场A掉..sad... 这题就是二分最小容量,对满足容量的加边,对时间求最短路.如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止. 代码如下: #include <iostrea

HDU Today(三种写法)(最短路)

Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市?浦镇陶姚村买了个房子,开始安度晚年了. 这样住了一段时间,徐总对当地的交通还是不太了解.有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格). 徐总经常会问蹩脚的英文问路:"Can you h

hdu 3790 最短路径问题(两个限制条件的最短路)

http://acm.hdu.edu.cn/showproblem.php?pid=3790 有两个条件:距离和花费.首先要求距离最短,距离相等的条件下花费最小. dijkstra,只是在判断条件时多考虑了花费. 注意重边. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h>