hdu3665-Seaside(SPFA,dijkstra,floyd)

Seaside

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1369    Accepted Submission(s): 984


Problem Description
XiaoY is living in a big city, there are N towns in it and some towns near the sea. All these towns are numbered from 0 to N-1 and XiaoY lives in the town numbered ’0’. There are some directed roads connecting them. It is guaranteed that you can reach any town from the town numbered ’0’, but not all towns connect to each other by roads directly, and there is no ring in this city. One day, XiaoY want to go to the seaside, he asks you to help him find out the shortest way.
 

Input
There are several test cases. In each cases the first line contains an integer N (0<=N<=10), indicating the number of the towns. Then followed N blocks of data, in block-i there are two integers, Mi (0<=Mi<=N-1) and Pi, then Mi lines followed. Mi means there are Mi roads beginning with the i-th town. Pi indicates whether the i-th town is near to the sea, Pi=0 means No, Pi=1 means Yes. In next Mi lines, each line contains two integers S Mi and L Mi, which means that the distance between the i-th town and the S Mi town is L Mi.
 

Output
Each case takes one line, print the shortest length that XiaoY reach seaside.
 

Sample Input
  
  
5 1 0 1 1 2 0 2 3 3 1 1 1 4 100 0 1 0 1
 

Sample Output
  
  
2
 



最短路径,话不多说,SPFA,dijkstra,floyd各贡献一枚!


dijkstra:


#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int map[15][15],dis[1000],vis[1000],target[15];
int N;
void dijkstra()
{
	memset(dis,INF,sizeof(dis));
	int i,j;
	for(i=0;i<N;i++)
	{
		dis[i]=map[0][i];
		vis[i]=0;
	}
	dis[0]=0;
	vis[0]=1;
	for(i=0;i<N;i++)
	{
		int mi=INF,mark=-1;
		for(j=0;j<N;j++)
		{
			if(!vis[j]&&dis[j]<mi)
			{
				mi=dis[j];
				mark=j;
			}
		}
		if(mark==-1)
		break;
		vis[mark]=1;
		for(j=0;j<N;j++)
		{
			if(!vis[j]&&dis[j]>dis[mark]+map[mark][j])
			dis[j]=dis[mark]+map[mark][j];
		}
	}
}
int main()
{
	int n,a,b,d,i,j,m;
	while(scanf("%d",&N)!=EOF)
	{
		int t=0;
		memset(map,INF,sizeof(map));
		for(i=0;i<N;i++)
		{
			scanf("%d%d",&m,&n);
			if(n)
			target[t++]=i;
			for(j=0;j<m;j++)
			{
				scanf("%d%d",&a,&b);
				map[i][a]=map[a][i]=b;
			}
		}
		dijkstra();
		int s[15];
		for(i=0;i<t;i++)
			s[i]=dis[target[i]];
		sort(s,s+t);
		printf("%d\n",s[0]);
	}
	return 0;
}


floyd:


#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int target[1000],map[15][15];
int N;
void floyd()
{
	int i,k,j;
	for(k=0;k<N;k++)
	{
		for(i=0;i<N;i++)
		{
			if(map[k][i]==INF)
			continue;
			for(j=0;j<N;j++)
			{
				if(map[i][j]>map[i][k]+map[k][j])
				map[i][j]=map[i][k]+map[k][j];
			}
		}
	}
}
int main()
{
	int n,i,j,a,d,m;
	while(scanf("%d",&N)!=EOF)
	{
		int t=0;
		memset(map,INF,sizeof(map));
		for(i=0;i<N;i++)
		{
			scanf("%d%d",&m,&n);
			if(n)
			target[t++]=i;
			for(j=0;j<m;j++)
			{
				scanf("%d%d",&a,&d);
				map[i][a]=map[a][i]=d;
			}
		}
		floyd();
		int q=INF;
		for(i=0;i<t;i++)
			q=q>map[0][target[i]]?map[0][target[i]]:q;
		printf("%d\n",q);		
	}
	return 0;
 } 


SPFA:


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int head[20],target[15],vis[20],dis[20];
int N,t,w;
struct node 
{
	int from,to,weight,next;
}s[1000];
void spfa()
{
	memset(vis,0,sizeof(vis));
	memset(dis,INF,sizeof(dis));
	queue<int> q;
	q.push(0);
	vis[0]=1;
	dis[0]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		dis[0]=0;
		for(int k=head[u];k!=-1;k=s[k].next)
		{
			int v=s[k].to;
			if(dis[v]>dis[u]+s[k].weight)
			{
				dis[v]=dis[u]+s[k].weight;
				if(!vis[v])
				{
					q.push(v);
					vis[v]=1;
				 } 
			}
		}
		
	}
	int qw[1000];
	for(int i=0;i<t;i++)
	qw[i]=dis[target[i]];
	sort(qw,qw+t);
	printf("%d\n",qw[0]);
}
int main()
{
	int n,a,b,d,i,j,m;
	while(scanf("%d",&N)!=EOF)
	{
		 t=0,w=0;
		memset(head,-1,sizeof(head));
		for(i=0;i<N;i++)
		{
			scanf("%d%d",&m,&n);
			if(n)
			target[t++]=i;
			for(j=0;j<m;j++)
			{
				scanf("%d%d",&a,&b);
				s[w].from=i;
				s[w].to=a;
				s[w].weight=b;
				s[w].next=head[i];
				head[i]=w++;
				s[w].from=a;
				s[w].to=i;
				s[w].weight=b;
				s[w].next=head[a];
				head[a]=w++;
			}
		}
		spfa();
	}
	return 0;
}

三种方法,各有利弊,但是floyd无疑是最简单的,三重for循环就行了,不过时间复杂度挺高的,容易超时,但这个题,0ms没问题!

有什么问题希望大家指出!谢谢!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值