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. 【noi 2.6_9272】偶数个数字3(DP)

    题意:问所有的N位数中,有多少个有偶数个数字3的数. 解法:f[i][j]表示i位数中含数字3的个数模2为j的个数.于是分第i位填3还是不填3讨论. 小tip:要模12345:for循环新定义了一个变 ...

  2. hoj2430 Counting the algorithms

    My Tags   (Edit)   Source : mostleg   Time limit : 1 sec   Memory limit : 64 M Submitted : 725, Acce ...

  3. codeforces 875B

    B. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...

  4. Petrozavodsk Summer Training Camp 2016H(多标记线段树)题解

    题意: \(n\)个草,第\(0\)天种下,高度都为\(0\),每个草每天长高\(a_i\).现给出\(q\)询问,每次给出第\(b_i\)天,然后把高于\(d_i\)的全削成\(d_i\),每次问你 ...

  5. hdu1228双指针

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; char ...

  6. Flow All In One

    Flow All In One Flow is a static type checker for JavaScript https://github.com/facebook/flow https: ...

  7. 1+X 证书制度

    1+X 证书制度 教育部职业技术教育 http://www.cvae.com.cn/zgzcw/tzgg/202001/c0ddd6c87e6c42839f8cc3e09a2dce89.shtml 2 ...

  8. free online business card generator

    free online business card generator 免费在线名片生成器 https://www.logaster.cn/business-card/ https://www.chu ...

  9. 「NGK每日快讯」11.24日NGK公链第22期官方快讯!

  10. JULLIAN MURPHY:拥有良好的心态,运气福气便会自来

    JULLIAN MURPHY是星盟全球投资公司的基金预审经理,负责星盟投资项目预审,有着资深的基金管理经验,并且在区块链应用的兴起中投资了多个应用区块链技术的公司. JULLIAN MURPHY认为往 ...