寒假集训图论B/J/L(最短路问题/SPFA算法)

题目
在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

Input
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
Output
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
Sample Input
2 1
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
Sample Output
3
2
解题思路
最短路模板题。。。
以下为SPFA代码
代码

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF=999999999;
int n,m;
int r[10101];
bool v[10101];
int a[202][202];
queue <int> Q;
void SPFA()
{
	r[1]=0;
	v[1]=true;
	Q.push(1);
	while (!Q.empty())
	{
		int x=Q.front();
		Q.pop();
		v[x]=false;
		for (int i=1;i<=n;i++)
		  {
		  	if (a[x][i]==INF || r[x]+a[x][i]>r[i]) continue;
		  	r[i]=r[x]+a[x][i];
		  	if (!v[i])
		  	{
		  		v[i]=true;
		  		Q.push(i);
		  	}
		  }
	}
}
int main()
{
    while (true)
	{
		scanf("%d%d",&n,&m);
		if (n==0 && m==0) break;
		for (int i=1;i<=n;i++)
		  for (int j=1;j<=n;j++)
		    a[i][j]=INF;
		for (int i=1;i<=n;i++) r[i]=INF;
		memset(v,false,sizeof(v));    	
		for (int i=1;i<=m;i++)
		  {
		  	int x,y,v;
		  	scanf("%d%d%d",&x,&y,&v);
		  	a[x][y]=min(a[x][y],v);
		  	a[y][x]=min(a[y][x],v);
		  }
		SPFA();
		printf("%d\n",r[n]);  
	}
}

题目
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。

现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1

#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int INF=999999999;
queue <int> Q;
int a[300][300];
int r[300];
int n,m;
bool v[300];
int S,T;
void SPFA()
{
    Q.push(S);
	v[S]=true;
	r[S]=0;
	while (!Q.empty())
	{
		int x=Q.front();
		v[x]=false;
		Q.pop();
		for (int i=0;i<n;i++) 
		  {
		  	if (a[x][i]==INF || r[x]+a[x][i]>r[i]) continue;
		  	r[i]=r[x]+a[x][i];
		  	if (!v[i]) 
		  	{
		  		Q.push(i);
		  		v[i]=true;
		  	}
		  }
	}
	
}
int main()
{
	while (~scanf("%d",&n))
	{
		scanf("%d",&m);
		memset(v,false,sizeof(v));
		for (int i=0;i<n;i++)
		  for (int j=0;j<n;j++)
		    a[i][j]=INF;
		for (int i=0;i<n;i++)
		  r[i]=INF;
		for (int i=1;i<=m;i++)
		  {
		  	int x,y,v;
		  	scanf("%d%d%d",&x,&y,&v);
		  	a[x][y]=min(a[x][y],v);
		  	a[y][x]=min(a[y][x],v);
		  }
		scanf("%d%d",&S,&T);
		SPFA(); 
		if (r[T]!=INF) printf("%d\n",r[T]); 
		else printf("-1\n");
	}
} 

题目
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input

  • Line 1: Two integers: T and N

  • Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.
    Output

  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
    Sample Input
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
    Sample Output
    90
    代码

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int INF=999999999;
queue <int> Q;
int a[1010][1010],d[1010];
bool sym[1010];
int n,m;
void SPFA()
{
	sym[1]=true;
	Q.push(1);
	d[1]=0;
	while (!Q.empty())
	{
		int x=Q.front();
		Q.pop();
		sym[x]=false;
		for (int i=1;i<=n;i++)
		  {
		  	if (d[x]+a[x][i]>d[i]) continue;
		  	d[i]=d[x]+a[x][i];
		  	if (!sym[i])
		  	{
		  		sym[i]=true;
		  		Q.push(i);
		  	}
		  }
		
	}
}
int main()
{
	scanf("%d%d",&m,&n);
	for (int i=1;i<=n;i++)
	  for (int j=1;j<=n;j++)
	    a[i][j]=INF;
	for (int i=1;i<=n;i++) d[i]=INF;    
	for (int i=1;i<=m;i++)
	  {
	  	int x,y,v;
	  	scanf("%d%d%d",&x,&y,&v);
	  	a[x][y]=min(a[x][y],v);
	  	a[y][x]=min(a[y][x],v);
	  }
	SPFA(); 
	printf("%d\n",d[n]); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值