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/ ...
随机推荐
- android自定义实现抽屉SlidingDrawer的功能
最近项目中需要实现上拉功能,首先想到的就是Android本身自带的抽屉SlidingDrawer,最后也实现了不过,出现的问题就是设置背景色问题,handler和content是两个不同的部分,这就造 ...
- ACM-最小生成树之畅通project——hdu1863
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- web.xml在<init-param>一个错误
Description ResourcePathLocationType cvc-complex-type.2.4.a: Invalid content was found starting with ...
- ACM-简单题之Factorial——poj1401
转载请注明出处:http://blog.csdn.net/lttree Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submis ...
- 【剑指offer】二叉树的镜像
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25915971 题目描写叙述: 输入一个二叉树,输出其镜像. 输入: 输入可能包括多个測试例 ...
- oracle事务(转)
今天温习oracle事务,记录如下: 事务定义 事务是保持数据的一致性,它由相关的DDL或者DML语句做为载体,这组语句执行的结果要么一起成功,要么一起失败. 我们 ...
- oracle 通过查询灵活插入数据 insert into ...select..
insert into reg_user (id,name,password,area_code,reg_time,first_pswd,record_type) select l.reg_user_ ...
- c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET
c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET c++ 如何获取系统时间 分类: C/C++ 2008-05-08 22:15 14115人阅读 评论(5 ...
- Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架
Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的. 历史 Bootstrap 是由 Twitter 的 ...
- HTML转义字符大全(转)
1.常用转义字符 转义字符串(Escape Sequence)也称字符实体(Character Entity).在HTML中,定义转义字符串的原因有两个:第一个原因是像“<”和“>”这类符 ...