0
点赞
收藏
分享

微信扫一扫

Choose the best route 2680 (dijkstra,反向建图)

毅会 2023-04-20 阅读 33


Choose the best route


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



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


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define mx 0x3f3f3f3f
using namespace std;
int map[1010][1010],dis[1010],vis[1010];
int n,m,s;
void djs(int x)
{
	int i,j,k;
	memset(vis,0,sizeof(vis));
	for(i=1;i<=n;i++)
		dis[i]=mx;
		dis[x]=0;
	while(1)
	{
		k=-1;
		for(i=1;i<=n;i++)
		{
			if(!vis[i]&&(k==-1||dis[i]<dis[k]))
			k=i;
		}
		if(k==-1)
			break;
		vis[k]=1;
		for(i=1;i<=n;i++)
			dis[i]=min(dis[i],dis[k]+map[k][i]);
	}
}
int main(){
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		int a,b,c,i,j;
		int n1,a1,b1;
		int mi;
		/*for(i=1;i<=n;i++)//两种初始化方式都行 
		for(j=1;j<=n;j++)
		{
			if(i==j)
				map[i][j]=0;
			else
				map[i][j]=mx;
		}*/
		memset(map,mx,sizeof(map));
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(map[b][a]>c)  //重点,只能从b到a单项赋值,因为车是单向走的,并且是反向建图 
				map[b][a]=c;	
		}
		
		scanf("%d",&n1);
		mi=mx;
		djs(s);
		while(n1--)
		{
			scanf("%d",&a1);			
			if(dis[a1]<mi)
				mi=dis[a1];
		}
		if(mi==mx)
			printf("-1\n");
		else
			printf("%d\n",mi);
	}
	return 0;
}

<pre class="cpp" name="code">//SPFA解
 
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 0x3f3f3f3f
#include<algorithm>
using namespace std;
int dis[1100],vis[1100];
struct Edge
{
	int from,to,val,next;
}edge[20100];
int head[20100],edgenum;
void add(int u,int v,int w)
{
	Edge E={u,v,w,head[u]};
	edge[edgenum]=E;
	head[u]=edgenum++;
}
void SPFA(int x)
{
	queue<int>q;
	memset(vis,0,sizeof(vis));
	memset(dis,INF,sizeof(dis));
	q.push(x);
	dis[x]=0;
	vis[x]=1;
	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(dis[v]>dis[u]+edge[i].val)
			{
				dis[v]=dis[u]+edge[i].val;
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
}
int n,m,s;
int main(){
	while(scanf("%d%d%d",&n,&m,&s)!=EOF)
	{
		int a,b,c,n1,a1,mm;
		memset(head,-1,sizeof(head));
		edgenum=0;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			add(b,a,c);
		}
		mm=INF;
		scanf("%d",&n1);
		SPFA(s);
		while(n1--)
		{
			scanf("%d",&a1);
			mm=min(mm,dis[a1]);
		}
		if(mm==INF)
			printf("-1\n");
		else
			printf("%d\n",mm);
	}
	return 0;
}






举报

相关推荐

0 条评论