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 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的更多相关文章
- As long as Binbin loves Sangsang
题目连接 题意: 给定一个无向图,每一个边有两个属性.长度和一个字母'L','O','V'.'E'中的一个.从1点開始到达n点,每次必须依照L -> O -> V -> E -> ...
- HDU 4360
题意很好理解. 由于点是可以重复到达的,但可能每次经过路径的标志不一样,所以可以设每个点有四种状态"L”,'O','V','E'.然后按这些状态进行求最短路,当然是SPFA了. #inclu ...
- POJ 3835 & HDU 3268 Columbus’s bargain(最短路 Spfa)
题目链接: POJ:http://poj.org/problem?id=3835 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3268 Problem ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)
题目链接: 传送门 畅通工程续 Time Limit: 1000MS Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
- HDU 2544 最短路 (最短路,spfa)
题意:中文题目 思路:spfa+SLF优化.关于SPFA的详情请戳我 #include <bits/stdc++.h> using namespace std; , INF=0x7f7f7 ...
- HDU 2992 Hotel booking(BFS+DFS 或者 SPFA+Floyd)
点我看题目 题意 : 一个司机要从1点到达n点,1点到n点中有一些点有宾馆,司机的最长开车时间不能超过10小时,所以要在10小时之内找到宾馆休息,但是为了尽快的走到n点,问最少可以经过几个宾馆. 思路 ...
- HDU 2722 Here We Go(relians) Again (spfa)
Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/ ...
随机推荐
- 用C++设计一个不能被继承的类(用私有构造函数+友元函数)
题目:用C++设计一个不能被继承的类. 分析:这是Adobe公司2007年校园招聘的最新笔试题.这道题除了考察应聘者的C++基本功底外,还能考察反应能力,是一道很好的题目. 在Java中定义了关键字f ...
- windows时间函数
介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记 ...
- 与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室
原文:与众不同 windows phone (30) - Communication(通信)之基于 Socket TCP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...
- 模板的Traits
Traits含义就是特性,应用Trait模板参数,使得我们的程序既保持灵活性,同时减少类型参数的数量.能够使得我们对函数进行更加细粒度的控制. #ifndef TRAIT_H_ #define TRA ...
- Redis 学习笔记五 经常使用php函数
PHPRedis的安装在这里: http://blog.csdn.net/xundh/article/details/46288277 键值操作 $redis = new Redis(); $redi ...
- c++
使用全局变量的方法多个文件
启动错误的做法 在global.h声明和定义变量 int sharedData = 9; 编译出错 Building target: CTest Invoking: GCC C++ Linker g+ ...
- Mybatis在oracle、mysql、db2、sql server的like模糊查询
<!-- oracle --> <select id="searchUserBySearchName" parameterType="java.lang ...
- AngularJs打造一个简易权限系统
AngularJs打造一个简易权限系统 一.引言 上一篇博文已经向大家介绍了AngularJS核心的一些知识点,在这篇博文将介绍如何把AngularJs应用到实际项目中.本篇博文将使用AngularJ ...
- Java程序员们最常犯的10个错误(转)
1.将数组转化为列表 将数组转化为一个列表时,程序员们经常这样做: 1 List<String> list = Arrays.asList(arr); Arrays.asList(&quo ...
- Eclipse插件引入jar包的方法
搞了两天,终于找到解决办法了.原来 Eclipse 插件项目引入外面的jar包不能用 build path---->add external jars的方法. 先说明两个概念:类加载器,O ...