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.