poj 1874 Tram

来源:互联网 发布:js打开网页弹出提示框 编辑:程序博客网 时间:2024/06/05 00:09
Tram
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 12808 Accepted: 4686

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 12 2 32 3 12 1 2

Sample Output

0
题目大意 :第一行 3 代表图中点的个数,2起点,1终点,下面三行是:第i行的第一个数是与第i个点相连的个数,本行的其他数字就是相连的点,第一个与该点相连的点距离为0,其他的点距离都为1;
 例如本例子的第二行 2(与第1个点相连的点个数) 2(与1点相离的距离为 0)3(与1点相离的距离为1);
练练模板:
   AC  floyd:
  
#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;#define N 105#define INF 0x3f3f3fint  T;int map[N][N];  int  Tnum,Temp;void  init(){int i,j;for(i=0;i<=N;i++){map[i][i]=0;for(j=i+1;j<=N;j++){map[i][j]=map[j][i]=INF;}}}void  floyd(int  B,int E){int i,j,k;for(i=1;i<=T;i++) for(j=1;j<=T;j++)  for(k=1;k<=T;k++)  {   map[j][k]=min(map[j][i]+map[i][k],map[j][k]);       }      if(map[B][E]==INF)      printf("-1\n");  else       printf("%d\n",map[B][E]);}int main(){ int  E; int B;while(scanf("%d%d%d",&T,&B,&E)!=EOF){init();for(int i=1;i<=T;i++){           scanf("%d",&Tnum);           for(int j=1;j<=Tnum;j++)           {           scanf("%d",&Temp);           if(j==1)           map[i][Temp]=0;           else           map[i][Temp]=1;   }}floyd(B,E);}return  0;} 

AC  Dijkstra
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;#define N 105#define INF 0x3f3f3fint s,e,t;int map[N][N],dist[N],vis[N];void init(){int  i,j;for(i=0;i<=t;i++)  {  map[i][i]=0;  for(j=0;j<=t;j++)  {  map[i][j]=map[j][i]=INF;  }  }}void dj(){int i,j;int  mindist,next;for(i=1;i<=t;i++)dist[i]=map[s][i];memset(vis,0,sizeof(vis));vis[s]=1;for(i=2;i<=t;i++) { mindist=INF; for(j=1;j<=t;j++) { if(dist[j]<mindist&&!vis[j]) { next=j; mindist=dist[j]; } } vis[next]=1; for(j=1;j<=t;j++) { if(dist[j]>dist[next]+map[next][j]&&!vis[j]) dist[j]=dist[next]+map[next][j]; } } if(dist[e]==INF) printf("-1\n"); else printf("%d\n",dist[e]);}int main(){while(scanf("%d%d%d",&t,&s,&e)!=EOF){init();int i,j,temp,a;for(i=1;i<=t;i++){scanf("%d",&temp);for(j=1;j<=temp;j++){   scanf("%d",&a);if(j==1)    map[i][a]=0;    else    map[i][a]=1;}}dj();}return 0; } 

spfa
  
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;#include<queue>#define N 200#define M N*N/2#define INF 0x3f3f3f3fint t,s,e,top;int  vis[N],dist[N],head[N];struct node{int from,to,next,val;}edge[M];void  init(){int  i,j;top=0;for(i=0;i<=t;i++){vis[i]=0;dist[i]=INF;head[i]=-1;}}void  addedge(int  u,int v,int w){edge[top].from=u;edge[top].to =v;edge[top].val =w;edge[top].next =head[u];head[u]=top++;}void  getmap(){int  i,j,temp,a;top=0;    for(i=1;i<=t;i++)    {    scanf("%d",&temp);    for(j=1;j<=temp;j++)    {    scanf("%d",&a);    if(j==1)    addedge(i,a,0);    else    addedge(i,a,1);}}}void spfa(){queue<int>Q;Q.push(s);vis[s]=1;dist[s]=0;while(!Q.empty() ){int u=Q.front() ;Q.pop();vis[u]=0;for(int  i=head[u];i!=-1;i=edge[i].next ){int v=edge[i].to ;if(dist[v]>dist[u]+edge[i].val ){ dist[v]=dist[u]+edge[i].val;  if(!vis[v]){vis[v]=1;Q.push(v);}    }}}if(dist[e]==INF)    printf("-1\n");    else    printf("%d\n",dist[e]);}int main(){while(scanf("%d%d%d",&t,&s,&e)!=EOF){init();getmap();spfa();}return 0; } 

0 0