Choose the best route


Problem Description

          One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
 

Input


          There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.

 

Output


         The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
 

Sample Input


5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1
 

Sample Output

 
1
-1

 

 

注意:在图论中注意重边问题是必须的,有向无向也是同等重要的,如这道题 from station p to station q说的就很清楚是有向图。

 

法一:加入超级源点。因为有多个起点可以选择,但只有一个终点,所以自己可以再加上一个点作为起点,编号为0,这个点和题中给的那些起点之间的距离为0。这样题目就转化为了求单源最短路径问题。

 不知道为什么这个没有AC ??

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f
#define  MIN(a,b) a<b?a:b
int d[1100];
int mark[1100];
int cost[1100][1100];
int n,m,s;
void dijkstra(int t)//因为我们多加了一个点,所以现在点的个数是n+1。 
{
	int i,j,u,v;
	for(i=0;i<=n;i++)
	{
		d[i]=INF;
		mark[i]=0;
	}
	d[0]=0;
	while(1)
	{
		v=-1;
		for(u=0;u<=n;u++)
		{
			if(mark[u]==0&&(v==-1||d[u]<d[v]))
			  v=u;
		}
		if(v==-1)
		  break;
		mark[v]=1;
		for(u=0;u<=n;u++)
		  d[u]=MIN(d[u],d[v]+cost[v][u]);
	}
}
int main()
{
	while(scanf("%d%d",&n,&m,&s)!=EOF)
	{
		int i,j;
		int num,p;
		int a,b,c;
		for(i=1;i<=n;i++)
		  for(j=1;j<=n;j++)
		    cost[i][j]=INF;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(c<cost[a][b])
			  cost[a][b]=c;//如果出现相同的一条路,选择短的那一条 
		}
		
		scanf("%d",&num);
	    while(num--)
		//for(i=0;i<num;i++)
		{
			scanf("%d",&p);//p是起点 
			cost[0][p]=0;//让加入的起点和真正的起点距离为零。 
	    }
		dijkstra(0);
		
		if(d[s]<INF)
		  printf("%d\n",d[s]);
		else
		  printf("-1\n");
	}
	return 0;
}


法二:起点有多个,终点只有一个,所以把起点与终点替换,反向建图

 

 AC

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f
#define MIN(a,b) a<b?a:b
int n,m,s;
int mark[1220];
int cost[1220][1220];
int d[1220];
void dijkstra(int t)
{
	int i,j,u;
	for(i=1;i<=n;i++)
	{
		d[i]=INF;
		mark[i]=0;
	}
	d[t]=0;
	while(1)
	{
		int v=-1;
		for(u=1;u<=n;u++)
		{
			if(mark[u]==0&&(v==-1||d[u]<d[v]))
			  v=u;
		}
		if(v==-1)
		  break;
	    mark[v]=1;
		for(u=1;u<=n;u++)
		  d[u]=MIN(d[u],d[v]+cost[v][u]);	
	}
} 
int main()
{
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		int a,b,c,i,j;
		for(i=1;i<=n;i++)
		  for(j=1;j<=n;j++)
		    cost[i][j]=INF;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(c<cost[b][a])//考虑重边 
			cost[b][a]=c;//<span style="color:#ff0000;">反向建图 
</span>		}
		dijkstra(s);//s是终点,现在把它当成起点 
		int num,min=INF,p;
		scanf("%d",&num);
		while(num--)
		{
			scanf("%d",&p);
			if(d[p]<min)
			min=d[p];//min用来保存最小值 
			
		}
		if(min<INF)
		  printf("%d\n",min);
		else
		  printf("-1\n");
	}
	return 0;
}


        输入n,m,s,代表有n个点,m条边,s代表终点。然后输入边,每条边输入p,q,t;p,q代表两个点,t表示边权,注意题目中说是从p指向q边,故应建立有向图。然后输入w表示有w个起点,然后输入起点;对于多个起点,如果对每个起点使用一次SPFA,则起点多的话可能会有点麻烦,所以不妨这样进行处理:另外设置一个不与题目中重合点,将所有起点到此点的边权设为0,然后以此点为起点进行查找所找到的必然是最短的   

  SPAF+超级源点+有向图

 

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f
#define  MAX 2100
using namespace std;
int d[MAX];
int head[MAX];
int mark[MAX]; 
int n,m,s;
int edgenum;
struct Edge
{
	int from,to,val,next;
}edge[MAX*10];
void addedge(int u,int v,int w)
{
	Edge E={u,v,w,head[u]};
	edge[edgenum]=E;
	head[u]=edgenum++;
}
void SPFA(int sx)
{
	memset(mark,0,sizeof(mark));
	memset(d,INF,sizeof(d));
	queue<int>q;
	q.push(sx);
	mark[sx]=1;
	d[sx]=0;
	while(!q.empty())
	{
		int u=q.front();
		    q.pop();
		    mark[u]=0;
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			int v=edge[i].to;
			if(d[v]>d[u]+edge[i].val)
			{
				d[v]=d[u]+edge[i].val;
				if(mark[v]==0)
				{
					q.push(v);
					mark[v]=1;
				} 
			}
		}
	} 
<span style="color:#ff0000;">//	if(d[s]!=INF)原来错在这儿!!下次注意!
</span>     if(d[s]<INF)
	  printf("%d\n",d[s]);
	else
	  printf("-1\n");
	
}
int main()
{
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		memset(head,-1,sizeof(head));
		edgenum=0;
		int i,j,a,b,c;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			addedge(a,b,c);
		} 
		int w,p;
		  scanf("%d",&w);
		while(w--)
		{
			scanf("%d",&p);
			addedge(0,p,0);
		<span style="color:#009900;">//	addedge(p,0,0);
</span>		}
		SPFA(0);
	}
	return 0;
} 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值