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 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
随机推荐
- WPF应用程序启动的问题(自定义Main函数启动)
问题引入: 一般WPF创建之后可以直接运行并不需要编写Main函数指定入口,但是在开发的过程中会遇到一些情况需要自定义Main让WPF从指定的Main函数中进行启动,这样可能会更好控制一点.但是我们再 ...
- Normal Equation of Computing Parameters Analytically
Normal Equation Note: [8:00 to 8:44 - The design matrix X (in the bottom right side of the slide) gi ...
- HTML5开发移动web应用——SAP UI5篇(9)
之前我们对于app的构建都是基于显示的.如今我们来格式化一下,引入很多其它的SAP UI5组件概念.这使得APP的一个界面更有层次性.更像是一个手机应用的界面,而且更好地使用SAP UI5中提供的功能 ...
- 【苦读官方文档】2.Android应用程序基本原理概述
官方文档原文地址 应用程序原理 Android应用程序是通过Java编程语言来写.Android软件开发工具把你的代码和其它数据.资源文件一起编译.打包成一个APK文件,这个文档以.apk为后缀,保存 ...
- JSON入门之二:org.json的基本用法 分类: C_OHTERS 2014-05-14 11:25 6001人阅读 评论(0) 收藏
java中用于解释json的主流工具有org.json.json-lib与gson,本文介绍org.json的应用. 官方文档: http://www.json.org/java/ http://de ...
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
http://www.lai18.com/content/9755931.html Spring 容器(Spring 的上下文) https://my.oschina.net/jast90/blog/ ...
- 【19.46%】【codeforces 551B】ZgukistringZ
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【转】A* A星 算法 C语言 实现代码
http://blog.csdn.net/shanshanpt/article/details/8977512 关于A*算法,很早就想写点什么,可是貌似天天在忙活着什么,可事实又没有做什么,真是浮躁啊 ...
- [Angular] @ViewChildren and QueryLists (ngAfterViewInit)
When you use @ViewChildren, the value can only be accessable inside ngAfterViewInit lifecycle. This ...
- js进阶 9-15 多选框如何限制选中数目
js进阶 9-15 多选框如何限制选中数目 一.总结 一句话总结: 1.多选框如何限制选中数目? 没点击选择一次,来统计现在总共选了多少个,如果超出,就给onclick事件返回false,取消oncl ...