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. Codeforces Round #648 (Div. 2) A. Matrix Game

    题目链接:https://codeforces.com/contest/1365/problem/A 题意 给出一个 $n \times m$ 的网格,两人轮流选择一个所在行列没有 $1$ 的方块置为 ...

  2. Gym 102263 ArabellaCPC 2019 J - Thanos Power (DP,数学)

    题意:有一个整数\(n\),每次可以对加\(10^x\)或减\(10^x\),问最少操作多少次能得到\(n\). 题解:对于某一位上的数,我们可以从\(0\)加几次得到,或者从前一位减几次得到.所以对 ...

  3. 实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署

    基础架构 主机名 角色 IP地址 mfyxw10.mfyxw.com K8S代理节点1,zk1 192.168.80.10 mfyxw20.mfyxw.com K8S代理节点2,zk2 192.168 ...

  4. codeforces - 15C Industrial Nim(位运算+尼姆博弈)

    C. Industrial Nim time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  5. C++的memset

    1. 需要的头文件 C中为<memory.h> 或 <string.h> C++中为<cstring> void * memset ( void * ptr, in ...

  6. PicGo:搭建图床

    PicGo:搭建图床 PicGo 免费搭建个人图床工具PicGo: 支持Windows.MacOS 和 Linux 软件目前覆盖的图床有8个平台: SM.MS图床.腾讯云COS.GitHub图床.七牛 ...

  7. 指纹采集器Live 20R

    最近有个项目需要使用指纹采集器Live 20R,买来这个小玩意后不知道怎么用,看了一些教程和自己摸索了一下,才初步掌握了用的方法. 环境: 硬件:联想 小新 操作系统:Win 10 IDE:VS201 ...

  8. 微信公众号 & 付费阅读

    微信公众号 & 付费阅读 付费功能 付费阅读 付费功能使用说明 1.付费功能介绍 开通了付费功能的公众号,运营者可以在编辑时对原创文章的部分或全部内容设置收费.对于付费图文,用户未付费前可免费 ...

  9. js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit

    js in depth: Object & Function & prototype & proto & constructor & classes inher ...

  10. uniapp 万年历

    大量代码来至这里 <template> <view class="calendar-main"> <!-- 当前年月 --> <view ...