HDOJ 2680 Choose the best route (最短路变形 Dijkstra && SPFA)

Choose the best route

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


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
注 - 此题为:   HDOJ  2680  Choose the best route  (最短路 Dijkstra && SPFA)

说明:   最短路 Dijkstra  && SPFA(有向图)由于 是 多个 起点,而只有一个终点 ,每个起点循环一次 Dijkstra 算法  会超时,  所以 要交换 起点 和 终点,反向建图,  去重 取小

已AC代码:( Dijkstra )

#include<cstdio>
#define INF 0xfffffff
#define MIN(x,y) (x<y?x:y)

int n,m,s; // n 车站数,m 个操作,s 终点 
int map[1100][1100],vis[1100],d[1100];

void Dijkstra()  // 模板 
{
	int i,j;
	for(i=1;i<=n;++i)
	{
		vis[i]=0;
		d[i]=INF;
	}
	d[s]=0; // 将终点 看做 起点 
	while(1)
	{
		j=-1;
		for(i=1;i<=n;++i)
		{
			if(vis[i]==0&&(j==-1||d[i]<d[j]))
				j=i;
		}
		if(j==-1)
			break;
		vis[j]=1;
		for(i=1;i<=n;++i)
		{
			d[i]=MIN(d[i],d[j]+map[j][i]);
		}
	}
}

int main()
{
	int i,j,p,q,t,w;
	
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		for(i=1;i<=n;++i)  // 初始化 
			for(j=1;j<=n;++j)
				map[i][j]=INF;
		
		for(i=0;i<m;++i)
		{
			scanf("%d%d%d",&p,&q,&t);
			if(map[q][p]>t)  // 有向图  反向建图,交换 起点 和 终点 
			{
				map[q][p]=t;
			}
		}
		
		Dijkstra();
		
		int min=INF,st,k=0;
		scanf("%d",&w);
		
		for(i=0;i<w;++i)  // 交换 起点 和 终点 
		{
			scanf("%d",&st);
			if(min>d[st]) 
				min=d[st];
		}
		
		if(min==INF)
			printf("-1\n");
		else
			printf("%d\n",min);
	}
	return 0;
}

已AC代码:(SPFA

#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 20000+10
#define INF 0x3f3f3f
using namespace std;

struct Edge{
	int from,to,vel,next;
};
Edge edge[MAX];   // 邻接表 

int head[MAX],cnt;
int N,M,S;
int dist[MAX],vis[MAX];

void addedge(int u,int v,int w)  // 加边 
{
	edge[cnt].from=u;
	edge[cnt].to=v;
	edge[cnt].vel=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}

void SPFA(int st)   // 模板 
{
	queue<int>Q;
	memset(dist,INF,sizeof(dist)); // 初始化 
	memset(vis,0,sizeof(vis));

	Q.push(st);
	vis[st]=1;
	dist[st]=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].vel)
			{
				dist[v]=dist[u]+edge[i].vel;
				if(vis[v]==0)
				{
					vis[v]=1;
					Q.push(v);
				}
			}
		}
	}
}

int main()
{
	int i,j,a,b,c;
	while(scanf("%d%d%d",&N,&M,&S)!=EOF)
	{
		cnt=0;
		memset(head,-1,sizeof(head));
		while(M--)
		{
			scanf("%d%d%d",&a,&b,&c); // 反向建图  
			addedge(b,a,c);
		}
		
		SPFA(S);
		
		int min=INF;
		scanf("%d",&a);
		
		for(i=0;i<a;++i)  // 交换 起点 和 终点 
		{
			scanf("%d",&b);
			if(min>dist[b]) 
				min=dist[b];
		}
		
		if(min==INF)
			printf("-1\n");
		else
			printf("%d\n",min);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值