UVaLive 4128 Steam Roller (多决策最短路)
题意:给定一个图,r 根横线, c 根竖线。告诉你起点和终点,然后从起点走,每条边有权值,如果是0,就表示无法通行。走的规则是:如果你在下个路要转弯,会使这段路的时间加倍,但是如果一条路同时是这样,那么也只算两倍。起点和终点他们相连的第一条边也算两倍。问你最短时间。
析:把每个点拆成 8 个点(r, c, dir, doubled)分别下一步走哪个方向,是不是要加倍,然后每次枚举上一条,和新边,枚举上一边是不是加倍之后的,然后判断是不是要转弯,然后计算加不加倍,最后跑一次最短路,就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 80000 + 10;
const int maxm = 100 + 10;
const ULL mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
const int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;
const int inv[] = {2, 3, 0, 1};
int id[maxm][maxm][4][2], cnt;
int g[maxm][maxm][4]; int ID(int r, int c, int dir, int doubled){
int &x = id[r][c][dir][doubled];
return x == 0 ? x = ++cnt : x;
} bool cango(int r, int c, int dir){
if(!is_in(r, c)) return false;
return g[r][c][dir] > 0;
} struct Edge{
int from, to, dist;
};
struct HeapNode{
int d, u;
bool operator < (const HeapNode &p) const{
return d > p.d;
}
}; struct Dijkstra{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn]; void init(int n){
this-> n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int dist){
edges.pb((Edge){from, to, dist});
m = edges.sz;
G[from].pb(m-1);
} void dijkstra(int s){
priority_queue<HeapNode> pq;
ms(d, INF); d[s] = 0;
ms(done, 0);
pq.push((HeapNode){0, s});
while(!pq.empty()){
HeapNode x = pq.top(); pq.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist){
d[e.to] = d[u] + e.dist;
pq.push((HeapNode){d[e.to], e.to});
}
}
}
}
};
Dijkstra dij; int readint(){ int x; scanf("%d", &x); return x; } int main(){
int r1, c1, r2, c2, kase = 0;
while(scanf("%d %d %d %d %d %d", &n, &m, &r1, &c1, &r2, &c2) == 6 && n){
--r1, --r2, --c1, --c2;
for(int r = 0; r < n; ++r){
for(int c = 0; c < m-1; ++c)
g[r][c][RIGHT] = g[r][c+1][LEFT] = readint();
if(r != n-1) for(int c = 0; c < m; ++c)
g[r][c][DOWN] = g[r+1][c][UP] = readint();
}
dij.init(n * m * 8 + 1);
cnt = 0; ms(id, 0);
for(int dir = 0; dir < 4; ++dir) if(cango(r1, c1, dir)) // the edge of source
dij.addEdge(0, ID(r1+dr[dir], c1+dc[dir], dir, 1), g[r1][c1][dir] * 2);
FOR(r, 0, n) FOR(c, 0, m) FOR(dir, 0, 4) if(cango(r, c, inv[dir]))
FOR(newdir, 0, 4) if(cango(r, c, newdir)) FOR(doubled, 0, 2){
int x = r + dr[newdir];
int y = c + dc[newdir];
int v = g[r][c][newdir], newdoubled = 0;
if(dir != newdir){
if(!doubled) v += g[r][c][inv[dir]]; // the old edge double
newdoubled = 1; v += g[r][c][newdir]; // the new edge double
}
dij.addEdge(ID(r, c, dir, doubled), ID(x, y, newdir, newdoubled), v);
}
dij.dijkstra(0);
int ans = INF;
FOR(dir, 0, 4) if(cango(r2, c2, inv[dir]))
for(int doubled = 0; doubled < 2; ++doubled){
int v = dij.d[ID(r2, c2, dir, doubled)];
if(!doubled) v += g[r2][c2][inv[dir]];
ans = min(ans, v);
}
printf("Case %d: ", ++kase);
if(ans == INF) puts("Impossible");
else printf("%d\n", ans);
}
return 0;
}
UVaLive 4128 Steam Roller (多决策最短路)的更多相关文章
- UVALive 4128 Steam Roller(最短路(拆点,多状态))
题意:模拟了汽车的行驶过程,边上的权值为全速通过所消耗的时间,而起步(从起点出发的边).刹车(到终点结束的边).减速(即将拐弯的边).加速(刚完成拐弯的边)这四种不能达到全速的情况,消耗的时间为权值* ...
- UVALive 4128 Steam Roller 蒸汽式压路机(最短路,变形) WA中。。。。。
题意: 给一个由n*m个正方形格子组成的矩形,其中每个格子的边都是可以走的,长度给定,规定:如果在进入该路前需要拐弯,或者走完该路需要拐弯,都是需要付出双倍距离的(每条路最多算2倍).问从起点到终点的 ...
- UVa1078 Steam Roller——拆点+最短路
题目链接 思路 把每个点拆成\(5\)个点\(id(x,y),id(x,y)+n,id(x,y)+2*n,id(x,y)+3*n,id(x,y)+4*n\),分别表示到这个点时的方向为上,右,下,左和 ...
- 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...
- 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)
layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...
- Delivering Goods UVALive - 7986(最短路+最小路径覆盖)
Delivering Goods UVALive - 7986(最短路+最小路径覆盖) 题意: 给一张n个点m条边的有向带权图,给出C个关键点,问沿着最短路径走,从0最少需要出发多少次才能能覆盖这些关 ...
- UVALive 7302 (最短路)
Probelm Terrorists 题目大意 给一张n个点,m条边的无向图.共有q个询问,每次询问u到v的最短路. n <= 100000 , n-1 <= m <= n + 5 ...
- UVALive 6885 Flowery Trails 最短路枚举
题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路 ...
- UVALive 3661 Animal Run(最短路解最小割)
题意:动物要逃跑,工作人员要截断从START(左上角)到END(右下角)的道路,每条边权表示拦截该条道路需要多少工作人员.问最少需要多少人才能完成拦截. 通俗地讲,就是把图一分为二所造成消耗的最小值. ...
随机推荐
- IIS7Appcmd 命令详解
IIS7 Appcmd 命令详解 废话不说!虽然有配置界面管理器!但是做安装包的时候命令创建是必不可少的!最近使用NSIS制作安装包仔细研究了一下Appcmd的命令,可谓是功能齐全. 上网查了些资料, ...
- 深入理解Java虚拟机,intern
1,在java1.7下面,intern不再复制实例,只存第一个引用,也就是new出来的有可能和intern相同(第一次情况 2,平时的new已经暗含了一个常量池,所有不适合上面情况, 参考:https ...
- VirtualBox的端口映射其实很好理解
还是和以前百度的另一个知识点一样,我真不明白网上那些人要做什么,明明很简单的事,干嘛非要讲的那么复杂,就是为了让人觉得你很高手?很厉害? 名称:随便起的,基于好记的原则,你的什么应用在使用这一条端口转 ...
- 清理Visual Studio中VC++工程里不需要的文件
Visual Studio开发C++,工程的空间几M,几十M甚至几百M的长,生成的中间文件看的眼花缭乱,占空间不说,特别是备份拷贝代码时无奈的等待,有了这个脚本,好吧,整个世界清静了. @echo o ...
- Accept Job Offer Email Template
Accept Job Offer Email Template <Date> <Hiring Manager’s name> <Company name> < ...
- 简单神经网络TensorFlow实现
学习TensorFlow笔记 import tensorflow as tf #定义变量 #Variable 定义张量及shape w1= tf.Variable(tf.random_normal([ ...
- php 文件缓存类
//文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...
- 页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久总算解决了
页面白屏并且报错PHP Parse error: syntax error, unexpected end of file in 试了很久 啥短标记,打开,都试了 最简单的办法 是重新建立一个文件, ...
- mybatis 1 - 获取自增ID
1.环境: mybatis : 3.2.3 spring-mybatis: 1.2.1 mysql:5.5.29 实体: public class sys_user { private int us ...
- vlc相关命令行设置
1:改变VLC模块参数 http://tianxiaoma.blog.51cto.com/1501174/309519 ====================================== ...