首页 > 其他 > 详细

POJ1847:Tram(最短路)

时间:2019-02-04 12:09:46      阅读:240      评论:0      收藏:0      [点我收藏+]

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 = 105;
int A,B,n,k;
int d[N],head[N],vis[N];
struct Edge{
    int u,v,w,next ;
}e[N*N<<1];
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,0,sizeof(vis));d[s]=0;
    node now;
    now.d=0;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]=1;
        for(int i=head[u];i!=-1;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,-1,sizeof(head));
    for(int i=1,v;i<=n;i++){
        scanf("%d",&k);
        for(int j=1;j<=k;j++){
            scanf("%d",&v);
            if(j==1) adde(i,v,0);
            else adde(i,v,1);
        }
    }
    Dijkstra(A);
    if(d[B]==INF) cout<<-1;
    else cout<<d[B];
    return 0;
}

 

POJ1847:Tram(最短路)

原文:https://www.cnblogs.com/heyuhhh/p/10351595.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!