POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手。这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题。并且pre数组记录前驱节点,print_path()方法可用算法导论上的。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#define Mod 1000000007
#define INT 2147483647
#define pi acos(-1.0)
#define eps 1e-3
#define lll __int64
#define ll long long
using namespace std;
#define N 100007 const int INF = Mod;
vector<pair<int,int> > edge[N];
set<pair<int,int> > que;
int a[][],n;
int d[N],pre[N]; void print_path(int s,int v)
{
if(v == s)
printf("(%d, %d)\n",(s-)/,(s%+-)%);
else
{
print_path(s,pre[v]);
printf("(%d, %d)\n",(v-)/,(v%+-)%);
}
} int ok(int x,int y)
{
if(x >= && x < && y >= && y < )
return ;
return ;
} void SPFA()
{
int i;
for(i=;i<=n;i++)
d[i] = INF;
d[] = ;
que.insert(make_pair(d[],));
while(!que.empty())
{
int v = que.begin()->second;
que.erase(que.begin());
for(i=;i<edge[v].size();i++)
{
int to = edge[v][i].first;
int cost = edge[v][i].second;
if(d[v] + cost < d[to])
{
que.erase(make_pair(d[to],to));
d[to] = d[v] + cost;
pre[to] = v;
que.insert(make_pair(d[to],to));
}
}
}
print_path(,n);
} int main()
{
int i,j;
for(i=;i<;i++)
for(j=;j<;j++)
scanf("%d",&a[i][j]);
for(i=;i<;i++)
{
for(j=;j<;j++)
{
if(a[i][j] == )
{
int ka = i* + j + ,kb;
if(ok(i+,j) && a[i+][j] == )
{
kb = ka + ;
edge[ka].push_back(make_pair(kb,));
}
if(ok(i,j+) && a[i][j+] == )
{
kb = ka+;
edge[ka].push_back(make_pair(kb,));
}
if(ok(i-,j) && a[i-][j] == )
{
kb = ka-;
edge[ka].push_back(make_pair(kb,));
}
if(ok(i,j-) && a[i][j-] == )
{
kb = ka-;
edge[ka].push_back(make_pair(kb,));
}
}
}
}
n = ;
SPFA();
return ;
}
POJ 1847 Tram --set实现最短路SPFA的更多相关文章
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- 【POJ】3255 Roadblocks(次短路+spfa)
http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立 ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
随机推荐
- jquery重置html form
很多时候在ajax提交或者对话框隐藏之后,我们希望重置默认值以便下次打开对话框时保持干净. 因为jquery选择器返回的是list,并且没有对此提供reset方法,所以需要针对单个元素进行reset. ...
- 「C语言」C输出hello world!系统发生了什么?
本篇文章全部摘抄自学长博客供以后学习: http://efraim.me/2015/12/05/tech-linux-2015-12-05/ 排版因与博客园编辑器不同而稍作修改. 输出hello wo ...
- Android sdk manager不能更新下载缓慢的解决方法
通常情况下,下载Android SDK需要连接谷歌的服务器进行下载,由于国内水深火热的网络,速度基本为0.好在国内也有一个更新的镜像地址.本文章介绍如何在不FQ的情况下,使用国内镜像地址,更新andr ...
- HTML Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunmen ...
- 【JWPlayer】官方JWPlayer去水印步骤
在前端播放视频,现在用html5的video标签已经是一个不错的选择,不过有时候还是需要用StrobeMediaPlayback.JWPlayer这一类的flash播放器,JWPlayer的免费版本带 ...
- JS框架的一些小总结
闭包结构 为了防止和别的库的冲突,用闭包把整个框架安全地保护好. 我们待会的代码都写在里面.这里创建一个全局变量"window.O",就是在window对象里加个O,它等价于 &q ...
- android布局--Android fill_parent、wrap_content和match_parent的区别
来自:http://www.cnblogs.com/nikyxxx/archive/2012/06/15/2551390.html 三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q131-Q132)
Question 131You create a SharePoint site by using the Document Center site template.You need to ensu ...
- Swift面向对象基础(上)——Swift中的类和结构体(下)
学习来自<极客学院> import Foundation class User { var name:String var age:Int init(name:String,age:Int ...
- 敏捷软件开发:原则、模式与实践——第12章 ISP:接口隔离原则
第12章 ISP:接口隔离原则 不应该强迫客户程序依赖并未使用的方法. 这个原则用来处理“胖”接口所存在的缺点.如果类的接口不是内敛的,就表示该类具有“胖”接口.换句话说,类的“胖”接口可以分解成多组 ...