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. java.awt.event.MouseEvent鼠标事件的定义和使用 以及 Java Swing-JTextArea的使用

    最近发现一个CSDN大佬写的Java-Swing全部组件的介绍:Java Swing 图形界面开发(目录) JTextArea 文本区域.JTextArea 用来编辑多行的文本.JTextArea 除 ...

  2. python的threading的使用(join方法,多线程,锁threading.Lock和threading.Condition

    一.开启多线程方法一 import threading,time def write1(): for i in range(1,5): print('1') time.sleep(1) def wri ...

  3. Codeforces Round #643 (Div. 2) E. Restorer Distance (贪心,三分)

    题意:给你\(n\)个数,每次可以使某个数++,--,或使某个数--另一个++,分别消耗\(a,r,m\).求使所有数相同最少的消耗. 题解:因为答案不是单调的,所以不能二分,但不难发现,答案只有一个 ...

  4. 读js DOM编程艺术总结

    第一章主要介绍一些历史性问题,javascript是Netcape和sun公司合作开发的. 第二章JavaScript语法: 1,数据类型:(弱类型)字符串,数值,布尔值(只有true和false,不 ...

  5. sqlmap在https情况下的一个错误

    对于https网站,使用sqlmap可能会出现如下错误.使用–force-ssl无效. https证书有问题 方法 本地建立proxy.php,内容为 <?php $url = "ht ...

  6. Cortex-M3 内核中悬起标志位细节逻辑

    对于外设中断,如果通过NVIC_DisableIRQ(xxx)关闭对应NVIC里面的使能位,会导致对应中断Pend位置起,如果清除Pend位时不清外设的中断标志位将导致对应Pend位立刻再次置起.所以 ...

  7. Hive Tutorial 阅读记录

    Hive Tutorial 目录 Hive Tutorial 1.Concepts 1.1.What Is Hive 1.2.What Hive Is NOT 1.3.Getting Started ...

  8. AirPods Max 出厂激活是怎么回事

    AirPods Max 出厂激活是怎么回事 话说出厂激活是怎么检测出来的 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问! 原创文 ...

  9. GitHub Actions in Action

    GitHub Actions in Action https://lab.github.com/githubtraining/github-actions:-hello-world https://g ...

  10. reStructuredText(.rst) && read the docs

    Read the Docs   &&  reStructuredText (.rst)  && markdown 1. github master 分支,创建 docs ...