poj 1874 Tram

Tram
Time Limit: 1000MS Memory Limit: 30000K
Total 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 1
2 2 3
2 3 1
2 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 0x3f3f3f
	int  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 0x3f3f3f
int 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 0x3f3f3f3f
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值