Simple POJ 1847 Tram,dijkstra.

Source: Internet
Author: User

Description

Tram Network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there are 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 of the switch is pointing. If the driver wants to go some other, he/she have to manually the switch.

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

Write a program that would calculate the minimal number of switch changes necessary to travel from intersection A to Inters    Ection B. The problem is to find the shortest way, then build the edge I and the first connecting edge, the edge is 0, the other Benquan is 1. The code is as follows:
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn= the;Const intinf=10e8;intVIS[MAXN];voidDijkstra (intLowcost[],intCOST[][MAXN],intNintstart) {    intMINN,MINP;  for(intI=1; i<=n;++i) {Lowcost[i]=INF; Vis[i]=0; } Lowcost[start]=0;  for(intcas=1; cas<=n;++CAs) {Minn=INF; MINP=-1;  for(intI=1; i<=n;++i)if(!vis[i] && lowcost[i]<Minn) {Minn=Lowcost[i]; MINP=i; }        if(minp==-1)            return; VIS[MINP]=1;  for(intI=1; i<=n;++i)if(!vis[i] && cost[minp][i]!=-1&& lowcost[i]>lowcost[minp]+Cost[minp][i]) lowcost[i]=lowcost[minp]+Cost[minp][i]; }}intMAP1[MAXN][MAXN];intANS[MAXN];intn,a,b;intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);    intK,a;  for(intI=1; i<=maxn;++i) for(intj=1; j<=maxn;++j) Map1[i][j]=-1; scanf (" %d%d%d",&n,&a,&c);  for(intI=1; i<=n;++i) {scanf ("%d",&k); if(k==0)            Continue; scanf ("%d",&a); Map1[i][a]=0; --K;  while(k--) {scanf ("%d",&a); if(map1[i][a]==-1) Map1[i][a]=1; }    }    //for (int i=1;i<=n;++i)//for (int j=1;j<=n;++j)//cout<<map1[i][j]<< ";Dijkstra (ans,map1,n,a); printf ("%d\n", Ans[b]==inf? -1: Ans[b]); return 0;}
View Code

Simple POJ 1847 Tram,dijkstra.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.