天梯赛 L2-001 紧急救援 (25 分)单源最短路

C++

#include<bits/stdc++.h>

using namespace std;
const int N=505;
int n,m,s,d,INF=0x3f3f3f3f;
int a[N];
int g[N][N];
int dist[N],st[N],path[N],cnt[N],num[N];

void dijkstra(int u)
{
	for(int i=0;i<n;++i)dist[i]=INF;
	dist[u]=0;
	cnt[u]=1;
	path[u]=-1;
	num[u]=a[u];
	for(int i=0;i<n;++i)
	{
		int t=-1;
		for(int j=0;j<n;++j)
		{
			if(st[j]==0&&(t==-1||dist[j]<dist[t]))t=j;
		}
		st[t]=1;
		for(int j=0;j<n;++j)
		{
			if(st[j]==0)
			{
				if(dist[j]>dist[t]+g[t][j])
				{
					dist[j]=dist[t]+g[t][j];
					cnt[j]=cnt[t];
					num[j]=num[t]+a[j];
					path[j]=t;
				}
				else if(dist[j]==dist[t]+g[t][j])
				{
					cnt[j]+=cnt[t];
					if(num[j]<num[t]+a[j])
					{
						num[j]=num[t]+a[j];
						path[j]=t;
					}
				}
			}
		}
	}
}

int main()
{
	cin>>n>>m>>s>>d;
	for(int i=0;i<n;++i)scanf("%d",&a[i]);
	
	for(int i=0;i<n;++i)
	{
		for(int j=0;j<n;++j)
		{
			if(i==j)g[i][j]=0;
			else g[i][j]=INF;
		}
	}
	
	for(int i=0;i<m;++i)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		if(g[x][y]>z)
		{
			g[x][y]=z;
			g[y][x]=z;
		}
	}
	
	dijkstra(s);
	
	printf("%d %d\n",cnt[d],num[d]);
	
	int t=d;
	stack<int> stk;
	while(t!=-1)
	{
		stk.push(t);
		t=path[t];
	}
	
	while(!stk.empty())
	{
		if(stk.size()!=1)
			printf("%d ",stk.top());
		else
			printf("%d",stk.top());
		
		stk.pop();
		
	}	
	
	return 0;
} 

Java 有个样例会超时,因为时间限制和C++一样

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.locks.StampedLock;

public class Main
{
	static int n,m,s,d,N=505,INF=0x3f3f3f3f;
	static int g[][]=new int[N][N],a[]=new int[N];
	static int path[]=new int[N],cnt[]=new int [N],num[]=new int[N],dist[]=new int[N],st[]=new int[N];
	
	static void dijkstra(int u)
	{
		for(int i=0;i<N;++i)dist[i]=INF;
		dist[u]=0;
		cnt[u]=1;
		path[u]=-1;
		num[u]=a[u];
		for(int i=0;i<n;++i)
		{
			int t=-1;
			for(int j=0;j<n;++j)
			{
				if(st[j]==0&&(t==-1||dist[j]<dist[t]))t=j;
			}
			st[t]=1;
			for(int j=0;j<n;++j)
			{
				if(st[j]==0)
				{
					if(dist[j]>dist[t]+g[t][j])
					{
						dist[j]=dist[t]+g[t][j];
						cnt[j]=cnt[t];
						path[j]=t;
						num[j]=num[t]+a[j];
					}
					else if(dist[j]==dist[t]+g[t][j])
					{
						cnt[j]+=cnt[t];
						if(num[j]<num[t]+a[j])
						{
							path[j]=t;
							num[j]=num[t]+a[j];
						}
					}
				}
			}
		}
	}
	
	public static void main(String args[]) throws NumberFormatException, IOException
	{
		BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		String ss[]=in.readLine().split(" ");
		n=Integer.parseInt(ss[0]);
		m=Integer.parseInt(ss[1]);
		s=Integer.parseInt(ss[2]);
		d=Integer.parseInt(ss[3]);
		
		ss=in.readLine().split(" ");
		for(int i=0;i<n;++i)a[i]=Integer.parseInt(ss[i]);
		
		
		
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<n;++j)
			{
				if(i!=j)g[i][j]=INF;
			}
		}
		for(int i=0;i<m;++i)
		{
			ss=in.readLine().split(" ");
			int x,y,z;
			x=Integer.parseInt(ss[0]);
			y=Integer.parseInt(ss[1]);
			z=Integer.parseInt(ss[2]);
			g[x][y]=Math.min(g[x][y], z);
			g[y][x]=g[x][y];
		}
		

		dijkstra(s);
		
		out.println(cnt[d]+" "+num[d]);
		
		Stack<Integer> stk=new Stack<Integer>();
		int t=d;
		while(t!=-1)
		{
			stk.push(t);
			t=path[t];
		}
		while(!stk.isEmpty())
		{
			if(stk.size()!=1)
				out.print(stk.pop()+" ");
			else 
				out.print(stk.pop());
		}
		out.flush();
	}
}
posted @ 2022-11-17 23:03  林动  阅读(15)  评论(0编辑  收藏  举报