Tram

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 20116   Accepted: 7491

题目链接:http://poj.org/problem?id=1847

Description:

Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch.

When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually.

Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B.

Input:

The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N.

Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed.

Output:

The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".

Sample Input:

3 2 1
2 2 3
2 3 1
2 1 2

Sample Output:

0

题意:

给出一个有向图,给出一些固定的边,另外还给出一些可以翻转的边的初始状态,翻转需要1的花费。最后求从起点到终点的最小花费。

题解:

对于翻转的那些边添加反向边权值为1,然后跑A到B的最短路即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int A,B,n,k;
int d[N],head[N],vis[N];
struct Edge{
int u,v,w,next ;
}e[N*N<<];
int tot;
struct node{
int d,u;
bool operator < (const node &A)const{
return d>A.d;
}
};
void adde(int u,int v,int w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void Dijkstra(int s){
priority_queue <node> q;memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));d[s]=;
node now;
now.d=;now.u=s;
q.push(now);
while(!q.empty()){
node cur = q.top();q.pop();
int u=cur.u;
if(vis[u]) continue ;
vis[cur.u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
now.d=d[v];now.u=v;
q.push(now);
}
}
}
}
int main(){
scanf("%d%d%d",&n,&A,&B);
memset(head,-,sizeof(head));
for(int i=,v;i<=n;i++){
scanf("%d",&k);
for(int j=;j<=k;j++){
scanf("%d",&v);
if(j==) adde(i,v,);
else adde(i,v,);
}
}
Dijkstra(A);
if(d[B]==INF) cout<<-;
else cout<<d[B];
return ;
}

POJ1847:Tram(最短路)的更多相关文章

  1. POJ-1847 Tram( 最短路 )

    题目链接:http://poj.org/problem?id=1847 Description Tram network in Zagreb consists of a number of inter ...

  2. poj1847 Tram 最短路Dijkstra

    题目链接:http://poj.org/problem?id=1847 Dijkstra算法的模版应用 题意:给你N个点和起点终点,点与点有铁路,接下来的N行分别为点i的情况 第一个数字表示与该点连通 ...

  3. poj1847 Tram(最短路dijkstra)

    描述: Tram network in Zagreb consists of a number of intersections and rails connecting some of them. ...

  4. POJ1847 Tram

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20274   Accepted: 7553 Description ...

  5. poj1847 Tram(Dijkstra || Floyd || SPFA)

    题目链接 http://poj.org/problem?id=1847 题意 有n个车站,编号1~n,每个车站有k个出口,车站的出口默认是k个出口中的第一个,如果不想从默认出口出站,则需要手动选择出站 ...

  6. POJ1847 Tram SPFA算法变形

    原题地址:http://poj.org/problem?id=1847 Tram:有轨电车 这题就是构造一个有向无权图,然后每一个点都会有一个开关,这个开关指向他的其中一个出度.当途经这个点的时候,如 ...

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

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

  8. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  9. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

随机推荐

  1. ctf题目writeup(8)

    2019.2.11 南京邮电的ctf平台: 地址http://ctf.nuptzj.cn/challenges# 他们好像搭新的平台了...我注册弄了好半天... 1. 签到题,打开网址: 查看一下页 ...

  2. Python3 函数return

    # def logger(): # f = open("loge.txt","a") # f.write("2017-09-15 exec funct ...

  3. ccpc 2018 final G - Pastoral Life in Stardew Valley

    #include <iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  4. 按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分

    SELECT S# as 学生ID ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 数据库 ,(SELECT score FROM S ...

  5. jquery validation remote进行唯一性验证时只使用自定义参数,不使用默认参数

    在使用validation进行唯一性验证时,想各个模块写一个统一的方法,相统一参数名称,但是remote方法会默认把对应的参数传过去 如: 会把role.roleName默认作为变量提交过去 所以想自 ...

  6. c#根据ip获取城市地址

    用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...

  7. 【APUE】Chapter10 Signals

    Signal主要分两大部分: A. 什么是Signal,有哪些Signal,都是干什么使的. B. 列举了非常多不正确(不可靠)的处理Signal的方式,以及怎么样设计来避免这些错误出现. 10.2 ...

  8. iOS URL加解密

    URL加解密 背景介绍 iOS 下URL加解密,项目使用AFNetworking 虽然是使用HTTPS,但是从安全方面考虑,在很多情况下还是需要对url的参数进行加密的. 接口如 https://19 ...

  9. lessJs

    lessJs下载地址 ======== 简介 lessJs主要提供页面切换,页面管理的一个框架:less-ui.css 和 less-ui.js 是独立于less.js的,他们提供的是一组ui,包括消 ...

  10. mysql创建用户并手授权

    --创建用户CREATE USER 'test'@'%' IDENTIFIED BY 'test'; --授权GRANT ALL ON *.* TO 'test'@'%'; --修改密码SET PAS ...