kuangbin_ShortPath S (POJ 3169)
被cow类题目弄得有些炸裂 想了好久好久写了120多行 依然长跪不起发现计算约束条件的时候还是好多麻烦的地方过不去
然后看了看kuangbin的blog 都是泪啊 差分约束的方式做起来只要70多行啊炒鸡简洁有没有
Ps 给手写queue的实现方式跪一下 真是快的飞起
------------------Update-------------------
虽然当时也感叹了一下为什么用最短路的写法解最长路但是没深究 但是看了奚政巨巨的odp(什么鬼格式)之后发现原来一切都来自神奇的差分约束
约束条件是dist[i] - dist[j] <= c 等价于if(dist[i] > c + dist[j]) dist[i] 就被松弛成dist[j] + c 都是算计啊!
妙 真是妙(脑补鬼畜)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; const int MAXN = (int)(1e4 + );
const int MAXM = (int)(1e5 + ); int n, ml, md, dist[MAXN];
int size, head[MAXN], point[MAXM], val[MAXM], nxt[MAXM]; void add (int from, int to, int value)
{
val[size] = value;
point[size] = to;
nxt[size] = head[from];
head[from] = size++;
} int spfa()
{
int time[MAXN];//也许以后用cnt做名字比较好
bool vis[MAXN];
queue<int> q;
memset(vis, false, sizeof vis);
memset(dist, INF, sizeof dist);
memset(time, , sizeof time); q.push();
vis[] = true;
dist[] = ;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = nxt[i]){
if(dist[point[i]] > dist[u] + val[i]){
dist[point[i]] = dist[u] + val[i];
if(!vis[point[i]]){
vis[point[i]] = true;
q.push(point[i]);
if(++time[point[i]] > n) return -;
}
}
}
}
return dist[n] == INF ? - : dist[n];
} int main()
{
size = ;
memset(head, -, sizeof head); scanf("%d%d%d", &n, &ml, &md);
while(ml--){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
if(from > to) swap(from, to);
add(from, to, value);
}
while(md--){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
if(from < to) swap(from, to);
add(from, to, -value);
}
int ans = spfa();
printf("%d\n", ans);
return ;
}
kuangbin_ShortPath S (POJ 3169)的更多相关文章
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- POJ 3169.Layout 最短路
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11612 Accepted: 5550 Descripti ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
- kuangbin_ShortPath L (POJ 2502)
dij部分还是跟模板差不多的 但是这题的难点是处理输入 或者说理解题意 事实上每个点之间都是可以走的......WA了好几发就因为没意识到同一条路线上的各个站点之间居然也可以走得比车子快.... PS ...
- kuangbin_ShortPath J (POJ 1511)
其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...
- poj 3169 Layout (差分约束)
3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...
随机推荐
- linux之开发板与宿主机-GDB远程调试
平台: redhat9.0 下载 gdb-5.3.tar.gz 解压 gdb-5.3.tar.gz: #tar vzxf gdb-5.3.tar.gz 配置文件# ./configure - targ ...
- Oracle排序问题
关于oracle排序的几点认识: (1)oracle本身不具有任何默认排序功能,要想排序,必须使用order by,而order by后的数据行默认是asc(升序排列),要降序选择desc : (2) ...
- java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
- BZOJ 2111 排列计数
f[i]=f[l]*f[r]*C(size[l]+size[r],size[l]). 需要lucas. #include<iostream> #include<cstdio> ...
- PHP、C++的重载
首先明确一点:PHP重载是用在面向对象的类当中,而不支持函数重载. 这点与C++不一样,在C++当中,重载可以用于面向过程和面向对象,而且方法也不一样. 在C++中,重载适用于当函数名相同时,函数所需 ...
- Unix command 积累
UNIX is a multi-user multitasking-optimized operating system that can run on various hardware platfo ...
- Number Game_状态压缩
Description Christine and Matt are playing an exciting game they just invented: the Number Game. The ...
- iOS教程
iOS绘图教程: http://www.cocoachina.com/industry/20140115/7703.html iOS基础教程:--包含脚本 swift http://bbs.zxope ...
- 13、SQL基础整理(流程控制begin……end)
流程控制 begin ……end将一个语句块包含起来,中间可以写任何语句 格式: begin--开始 select *from student end--结束 if declare @bianlian ...
- 超链接访问过后hover样式就不出现的问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...