hdu4360 spfa+分割点
标题要求必须按照L O V E 行走为了,你必须至少有一个完整的LOVE。说明可以通过同一个点反复
对每一个点拆分为4个点。分别为从L,O,V,E到达。
起始点看做是从E到达的
spfa时发现当前点距离同样,比較经过的边数,此时若边数更大,也要入队列!由于要更新后面的点经过的边数
trick 是点能够有自环,当N = 1时
1 4
1 1 1 L
1 1 1 O
1 1 1 V
1 1 1 E
还有就是数据可能会超int
敲了两遍,第一遍dijsktra怎么測都对,trick也都想到了。就是WA到死,第二遍初始化手贱4写成3。
。。
附送好多组数据。。。
都是自己编的
//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm> #include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstdlib> using namespace std;
//LOOP
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s)
typedef long long LL;
const int MAXN = 1010; #define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
#define SZ(V) (int)V.size()
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
#define sqr(x) x * x
typedef vector <int> VI;
typedef unsigned long long ULL;
const double eps = 1e-10;
const LL MOD = 1e9 + 7; typedef long long LL;
using namespace std; const int maxn = 3010;
const LL INF = 1e18; struct Edge{
int from, to, dis, let;
Edge(int u, int v, int w, int let): from(u), to(v), dis(w), let(let) {}
}; struct Node{
int u, let;
Node(int d, int let): u(d), let(let) {}
}; int n, m;
LL d[maxn][5];
int num[maxn][5], inq[maxn][5];
VI G[maxn];
vector<Edge> edges; void init(int nn)
{
n = nn;
edges.clear();
FE(i, 0, n) G[i].clear();
} void addEdge(int u, int v, int w, int let)
{
edges.push_back(Edge(u, v, w, let));
m = edges.size();
G[u].push_back(m - 1);
} void spfa()
{
queue<Node> Q;
Q.push(Node(1, 3));
FE(i, 0, n) REP(j, 4) d[i][j] = INF;
CLR(num, 0), CLR(inq, 0);
d[1][3] = 0, inq[1][3] = 1;
while (!Q.empty())
{
Node x = Q.front(); Q.pop();
int u = x.u, let = x.let;
inq[u][let] = 0;
REP(i, G[u].size())
{
Edge& e = edges[G[u][i]];
if (e.let != (let + 1) % 4) continue;
if (d[e.to][e.let] > d[u][let] + e.dis || !d[e.to][e.let])
{
d[e.to][e.let] = d[u][let] + e.dis;
num[e.to][e.let] = num[u][let] + 1;
// printf("now:%d to:%d letter:%d num:%d d:%d\n", u, e.to, e.let, num[e.to][e.let], d[e.to][e.let]);
if (!inq[e.to][e.let])
{
inq[e.to][e.let] = 1;
Q.push(Node(e.to, e.let));
}
}
else if (d[e.to][e.let] == d[u][let] + e.dis && num[e.to][e.let] <= num[u][let])
{
num[e.to][e.let] = num[u][let] + 1;
if (!inq[e.to][e.let])
{
inq[e.to][e.let] = 1;
Q.push(Node(e.to, e.let));
}
}
}
}
} int main()
{
int T, N, M;
RI(T);
FE(kase, 1, T)
{
int x, y, z, let;
char s[20];
RII(N, M);
init(N);
REP(i, M)
{
RIII(x, y, z);
RS(s);
if (s[0] == 'L') let = 0;
if (s[0] == 'O') let = 1;
if (s[0] == 'V') let = 2;
if (s[0] == 'E') let = 3;
addEdge(x, y, z, let);
addEdge(y, x, z, let);
}
spfa();
printf("Case %d: ", kase);
// cout << d[N][3] << num[N][3] << endl;
if (d[N][3] == INF || num[N][3] / 4 < 1)
{
printf("Binbin you disappoint Sangsang again, damn it!\n");
continue;
}
printf("Cute Sangsang, Binbin will come with a donkey after travelling %I64d meters and finding %d LOVE strings at last.\n",
d[N][3], num[N][3] / 4);
}
return 0;
}
/*
55
6 6
1 2 10 L
2 3 20 O
2 4 30 O
3 5 30 V
4 5 10 V
5 6 10 E
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
1 0
2 1
1 2 1 E 2 8
1 1 2 L
1 1 1 O
1 1 1 V
1 1 1 E
1 2 3 L
2 1 1 O
1 2 1 V
2 1 1 E 12 12
1 5 5 L
5 6 5 O
6 7 5 V
7 12 5 E
1 2 1 L
2 3 1 O
3 4 1 V
4 8 1 E
8 9 1 L
9 10 1 O
10 11 1 V
11 12 13 E 23 24
1 5 5 L
5 6 5 O
6 7 5 V
7 12 5 E
1 2 1 L
2 3 1 O
3 4 1 V
4 8 1 E
8 9 1 L
9 10 1 O
10 11 1 V
11 12 13 E
12 13 1 L
13 14 1 O
14 15 1 V
15 16 1 E
16 17 1 L
17 18 1 O
18 19 1 V
19 23 13 E
12 20 5 L
20 21 5 O
21 22 5 V
22 23 5 E
*/
版权声明:本文博主原创文章。博客,未经同意不得转载。
hdu4360 spfa+分割点的更多相关文章
- 【BZOJ-3627】路径规划 分层图 + Dijkstra + spfa
3627: [JLOI2014]路径规划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 186 Solved: 70[Submit][Status] ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- sgu 240 Runaway (spfa)
题意:N点M边的无向图,边上有线性不下降的温度,给固定入口S,有E个出口.逃出去,使最大承受温度最小.输出该温度,若该温度超过H,输出-1. 羞涩的题意 显然N*H的复杂度dp[n][h]表示到达n最 ...
- spfa模板
通过stl的queue实现的spfa(vector实现邻接表存图) 本模板没有考虑存在两点不连通的情况 如果需要判断则需要用到并查集或者遍历整个邻接表 #include<iostream> ...
- SPFA
SPFA算法用来求单源最短路.可以处理任何有解的情况. 先建一个数组\(dist_x = 起点到x的最短路长度\),当\(x=起点\)时为0,当x和起点不通时为INF(本题中为\(2^31-1\)). ...
- BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=2763 Description Alice和Bob现在要乘飞机旅行,他们选择了一家 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- bzoj 1179[Apio2009]Atm (tarjan+spfa)
题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- codevs 1021 玛丽卡(spfa)
题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
随机推荐
- sublime text2 基本配置及结合Python 环境
参考: http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html http://www.zhihu.com/question/ ...
- Hadoop集群日常运维 分类: A1_HADOOP 2015-03-01 21:26 502人阅读 评论(0) 收藏
(一)备份namenode的元数据 namenode中的元数据非常重要,如丢失或者损坏,则整个系统无法使用.因此应该经常对元数据进行备份,最好是异地备份. 1.将元数据复制到远程站点 (1)以下代码将 ...
- php中模拟多继承如何实现
php中模拟多继承如何实现 一.总结 一句话总结:其实你继承别人也是想调用别人类里面的方法和属性,所以可以这样做:这本类中创建目标类的对象,然后通过这个对象来调用方法和属性,这样比继承来的方便. 二. ...
- null与对象的复杂关系(typeof null的结果是object的原因)
原文 简书原文:https://www.jianshu.com/p/c1608452d056 前言 对象是 JavaScript 的基础.在 JavaScript 中一共有六种主要类型(术语是“语言类 ...
- mariadb远程不能访问,出现Can't connect to MySQL server on '' (10061)
一,现象: 1. 1 远程连接数据库mariadb时,报错 二,定位: 2. 1 首先本地连接上数据库,然后操作权限表数据 ,然后远程再次连接依然连接不上: 2. 2 搜索mariadb的配置文 ...
- 计算git树上随意两点的近期切割点。
1.git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比方: base'<--base<--A<--A' ^ | --- B<--B' 小米project师 ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
- Android 在ListView中嵌套ListView的事件处理
前天在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Ite ...
- 【30.49%】【codeforces 569A】Music
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 10 in
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset 10 ...