Tram

POJ-1847

这里其实没有必要使用SPFA算法,但是为了巩固知识,还是用了。也可以使用dijikstra算法。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int INF=0X3F3F3F3F;
const int maxn=102;
int n,a,b;
int map[maxn][maxn];
bool vis[maxn];
int d[maxn];
struct edge{
int to;
int cost;
};
vector<edge> edges[maxn];
void SPFA(int s){
memset(vis,0,sizeof(vis));
vis[s]=1;
queue<int> q;
memset(d,INF,sizeof(d));
d[s]=0;
q.push(s);
while(!q.empty()){
int v=q.front();
q.pop();
vis[v]=0;
for(int j=0;j<edges[v].size();j++){
edge e=edges[v][j];
int u=e.to;
int cost=e.cost;
if(d[u]>d[v]+cost){
d[u]=d[v]+cost;
if(!vis[u]){
q.push(u);
vis[u]=1;
}
}
}
}
}
int main(){
while(cin>>n>>a>>b){
for(int i=1;i<=n;i++){
int k;
cin>>k;
for(int j=1;j<=k;j++){
int te;
cin>>te;
if(j==1){
edges[i].push_back({te,0});
map[i][te]=0;
}else {
map[i][te]=1;
edges[i].push_back({te,1});
}
}
}
SPFA(a);
if(d[b]==INF)
cout<<-1<<endl;
else
cout<<d[b]<<endl;
}
return 0;
}

java:

package POJ;
import java.util.*;
import java.io.*;
public class POJ_1847 {
static int n,a,b;
static final int INF=0X3F3F3F3F;
static class edge{
int to;
int cost;
edge(){}
edge(int to,int cost){
this.to=to;
this.cost=cost;
}
};
static Vector<edge>[] ed;
static int []d;
static class node implements Comparable<node>{
int to;
int dis;
node(){}
node(int to,int dis){
this.to=to;
this.dis=dis;
}
@Override
public int compareTo(node t) {//默认小根堆,越小优先级越大
// TODO Auto-generated method stub
if(dis<t.dis)
return -1;
else if(dis>t.dis)
return 1;
else return 0;
} };
static void dijkstra(int s) {
PriorityQueue<node>que=new PriorityQueue<node>();
que.add(new node(s,0));
Arrays.fill(d, INF);
d[s]=0;
while(!que.isEmpty()) {
node temp=que.poll();
if(d[temp.to]<temp.dis)
continue;
for(int i=0;i<ed[temp.to].size();i++) {
edge e=ed[temp.to].elementAt(i);
if(d[e.to]>d[temp.to]+e.cost) {
d[e.to]=d[temp.to]+e.cost;
que.add(new node(e.to,d[e.to]));
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
a=cin.nextInt();
b=cin.nextInt();
ed=new Vector[n+1];
for(int i=0;i<=n;i++) {
ed[i]=new Vector<edge>();
}
d=new int[n+1];
for(int i=1;i<=n;i++) {
int k;
k=cin.nextInt();
for(int j=0;j<k;j++) {
int temp;
temp=cin.nextInt();
if(j==0) {//默认指向的方向
ed[i].add(new edge(temp,0));
}else {
ed[i].add(new edge(temp,1));
}
}
}
dijkstra(a);
if(d[b]==INF)
System.out.println(-1);
else System.out.println(d[b]);
} }

POJ-1847(SPFA+Vector和PriorityQueue优化的dijstra算法)的更多相关文章

  1. Tram POJ - 1847 spfa

    #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #inc ...

  2. poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)

    /** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...

  3. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  4. poj 1511(SPFA+邻接表)

    题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...

  5. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  6. poj 1847 最短路简单题,dijkstra

    1.poj  1847  Tram   最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个 ...

  7. 图论之最短路径(3)队列优化的Bellman-Ford算法(SPFA算法)

    在Bellman-Ford算法中 我们可以看到大量的优化空间:如果一个点的最短路径已经确定了,那么它就不会再改变,因此不需要再处理.换句话说:我们每次只对最短路径改变了的顶点的所有出边进行操作 使用一 ...

  8. SPFA的两个优化:SLF与LLL

    先举出个例题:洛谷P3371 [模板]单源最短路径 一眼扫去:最短路径. spfa不接受反驳... 附上代码: #include<iostream> #include<algorit ...

  9. hiho一下 第二十九周 最小生成树三·堆优化的Prim算法【14年寒假弄了好长时间没搞懂的prim优化:prim算法+堆优化 】

    题目1 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生 ...

随机推荐

  1. Baby-step giant-step算法

    写在前面: 学习笔记,方便复习,学习资料来自网络,注明出处 我们都在努力奔跑,我们都是追梦人 结论 In group theory, a branch of mathematics, the baby ...

  2. Codeforces #624 div3 C

    You want to perform the combo on your opponent in one popular fighting game. The combo is the string ...

  3. std::invoke_result的实现详解

    目录 目录 前言 invoke_result 标准库中的invoke_result 我的实现 后记 前言 本篇博文将详细介绍一下libstdc++中std::invoke_result的实现过程,由于 ...

  4. codeforces 1009D Relatively Prime Graph【欧拉函数】

    题目:戳这里 题意:要求构成有n个点,m条边的无向图,满足每条边上的两点互质. 解题思路: 显然1~n这n个点能构成边的条数,就是2~n欧拉函数之和(x的欧拉函数值代表小于x且与x互质的数的个数. 因 ...

  5. liunx命令二

    声明:以下资料全部摘自实验楼 常用快捷键 按键 作用 Table 补全命令 Ctrl+c 强制结束 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂停当前程序,暂停后按下任意键恢复运行 Ctrl ...

  6. TypeScript constructor public cause duplicate bug

    TypeScript constructor public cause duplicate bug constructor public const log = console.log; // con ...

  7. CSS3 & gradient & color & background

    CSS3 & gradient & color & background css background https://developer.mozilla.org/en-US/ ...

  8. linux bash shell & lsof & grep & ps

    linux bash shell & lsof & grep & ps lsof list all open files # lsof & grep $ lsof -P ...

  9. dart 匹配基本map

    var map_start = RegExp(r'^\s*\{\s*'); var map_end = RegExp(r'^\}\s*(,)?\s*'); var hasComma = true; M ...

  10. SpringBoot进阶教程(七十一)详解Prometheus+Grafana

    随着容器技术的迅速发展,Kubernetes已然成为大家追捧的容器集群管理系统.Prometheus作为生态圈Cloud Native Computing Foundation(简称:CNCF)中的重 ...