题意:

给定n个点m条边的无向图

每次必须沿着LOVE走,到终点时必须是完整的LOVE,且至少走出一个LOVE,

问这样情况下最短路是多少,在一样短情况下最多的LOVE个数是多少。

有自环。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 ll;
const ll Inf = 4611686018427387904LL;
const int N = 1314 + 100;
const int E = 13520 * 2 + 100;
const int M = N * 4 + 100;
struct Edge {
ll len;
int v, f, nex;
Edge() {
}
Edge(int _v, int _f, ll _len, int _nex) {
v = _v;
f = _f;
len = _len;
nex = _nex;
}
};
struct node{
int to, f;
node(int b=0,int d=0):to(b),f(d){}
};
Edge eg[E];
ll dis[N][4], tim[N][4];
bool vis[N][4];
int T, n, g[N], idx; int re(char c) {
if (c == 'L')
return 0;
else if (c == 'O')
return 1;
else if (c == 'V')
return 2;
else
return 3;
}
void addedge(int u, int v, ll len, int f) {
eg[idx] = Edge(v, f, len, g[u]);
g[u] = idx++;
}
void spfa() {
memset(vis, 0, sizeof vis);
for (int i = 0; i < n; ++i)
for (int j = 0; j < 4; ++j) {
dis[i][j] = Inf;
tim[i][j] = 0;
}
queue<node>q;
q.push(node(0,3));
dis[0][3] = 0;
tim[0][3] = 0;
while(!q.empty()){
node u = q.front(); q.pop(); vis[u.to][u.f] = 0;
for(int i = g[u.to]; ~i; i = eg[i].nex){
int y = eg[i].v, f = eg[i].f;
if(f != (u.f+1)%4)continue;
bool yes = false;
if(dis[y][f] > dis[u.to][u.f]+eg[i].len)
{
dis[y][f] = dis[u.to][u.f]+eg[i].len;
tim[y][f] = tim[u.to][u.f];
if(f == 3)
tim[y][f]++;
yes = true;
}
else if(dis[y][f] == dis[u.to][u.f]+eg[i].len) {
ll tmp = tim[u.to][u.f];
if(f == 3)
tmp++;
if(tmp > tim[y][f])
tim[y][f] = tmp, yes = true;
}
else if(tim[y][f]==0) {
ll tmp = tim[u.to][u.f];
if(f == 3)
tmp++;
if(tmp > tim[y][f])
dis[y][f] = dis[u.to][u.f]+eg[i].len, tim[y][f] = tmp, yes = true;
}
if(yes && vis[y][f] == 0)
vis[y][f] = 1, q.push(node(y, f));
}
}
}
void work() {
int m, u, v; ll len;
char s[5];
memset(g, -1, sizeof g);
idx = 0; scanf("%d %d", &n, &m);
while (m -- > 0) {
scanf("%d%d%I64d%s", &u, &v, &len, s);
-- u; -- v;
addedge(u, v, len, re(s[0]));
addedge(v, u, len, re(s[0]));
}
spfa();
ll ansdis = dis[n - 1][3], ansnum = tim[n - 1][3];
printf("Case %d: ", ++T);
if (ansdis == Inf || ansnum == 0) {
puts("Binbin you disappoint Sangsang again, damn it!");
} else {
printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %I64d LOVE strings at last.\n", ansdis, ansnum);
}
}
int main() {
int cas;
T = 0;
scanf("%d", &cas);
while (cas -- > 0)
work();
return 0;
}
/*
99
4 4
1 2 1 L
2 4 1 O
4 1 1 V
1 4 1 E 1 4
1 1 1 L
1 1 1 O
1 1 1 V
1 1 1 E 1 0 */

HDU 4360 As long as Binbin loves Sangsang spfa的更多相关文章

  1. As long as Binbin loves Sangsang

    题目连接 题意: 给定一个无向图,每一个边有两个属性.长度和一个字母'L','O','V'.'E'中的一个.从1点開始到达n点,每次必须依照L -> O -> V -> E -> ...

  2. HDU 4360

    题意很好理解. 由于点是可以重复到达的,但可能每次经过路径的标志不一样,所以可以设每个点有四种状态"L”,'O','V','E'.然后按这些状态进行求最短路,当然是SPFA了. #inclu ...

  3. POJ 3835 &amp; HDU 3268 Columbus’s bargain(最短路 Spfa)

    题目链接: POJ:http://poj.org/problem?id=3835 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3268 Problem ...

  4. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  5. HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)

    题目链接: 传送门 畅通工程续 Time Limit: 1000MS     Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...

  6. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  7. HDU 2544 最短路 (最短路,spfa)

    题意:中文题目 思路:spfa+SLF优化.关于SPFA的详情请戳我 #include <bits/stdc++.h> using namespace std; , INF=0x7f7f7 ...

  8. HDU 2992 Hotel booking(BFS+DFS 或者 SPFA+Floyd)

    点我看题目 题意 : 一个司机要从1点到达n点,1点到n点中有一些点有宾馆,司机的最长开车时间不能超过10小时,所以要在10小时之内找到宾馆休息,但是为了尽快的走到n点,问最少可以经过几个宾馆. 思路 ...

  9. HDU 2722 Here We Go(relians) Again (spfa)

    Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/ ...

随机推荐

  1. C 编程最佳实践(书写风格)

    简介本文是为了满足开发人员的需要而写的.我们总结了一套指南,无论作为开发人员还是顾问,这些指南多年来一直都很好地指导着我们,我们把它们作为建议提供给您,希望对您的工作有所帮助.您也许不赞同其中的某些指 ...

  2. Linux目录结构和常用命令

    源地址:http://www.cnblogs.com/JCSU/articles/2770249.html 一.Linux目录结构 你想知道为什么某些程序位于/bin下,或者/sbin,或者/usr/ ...

  3. Centos 7 学习之静态IP设置

    原文链接:http://blog.csdn.net/johnnycode/article/details/40624403 本学习主要针对 Centos 7.0.1406 版本进行学习整理! 如果你使 ...

  4. jvm调优经验分享

    当Java程序申请内存,超出VM可分配内纯的时候,VM首先可能会GC,假设GC完还是不够,或者申请的直接超够VM可能有的,就会抛出内 存溢出异常.从VM规范中我们能够得到,一下几种异常. java.l ...

  5. 专注UI——有用技术:模糊搜索

    在如今的项目中.须要做模糊搜索,在曾经技术的基础上非常快得完毕了第一版.大家先看看第一版的效果,我们一会做评论: 0基础: 我们可能部分源代码(附件中会有所有源代码) <span style=& ...

  6. Delphi过程函数传递参数的八种方式

    今天一同事问我为什么有些过程函数里面有Var而有些没有,不解,遂到网上百度,得解.快哉,快哉. 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out.另一种不加修饰符的为默认按值传 ...

  7. Spark实践的阶段性总结

    写这篇小总结是因为前段时间是自己业余时间对Spark相关进行了些探索,接下来可能有别的同事一起加入,且会去借用一些别的服务器资源,希望可以借此理下思路. 实践Spark的原因 在之前Spark简介及安 ...

  8. Android OpenGL ES 应用(二) 纹理

    上一篇讲了基础入门 OpenGL (一) ,这一次主要学习OpenGL 纹理基本学习总结 要是做复杂的OpenGL应用程序,一定会用到纹理技术.纹理说白了就是把图片或者视频图像绘制到OpenGL空间中 ...

  9. xcode中找不到XXX.dylib

    xcode中找不到 XXX.dylib 了,比如libz.tbd 如果要用到 libz.dylib,可以用下面的办法,来自 Stack Overflow. Go to Build Phases > ...

  10. C#的百度地图开发(一)发起HTTP请求

    原文:C#的百度地图开发(一)发起HTTP请求 百度地图的开发文档中给出了很多的事例,而当用到具体的语言来开发时,又会有些差异.我是使用C#来开发的.在获取相应的数据时,需要通过URL传值,然后获取相 ...