Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用Dij,tle到死,请问怎么优化啊?

Posted by 0810311106 at 2010-09-25 22:10:08 on Problem 2449
#include"iostream"
#include"cstdio"
#include"string"
#define M 1001 
#define inf 1000000009 
using namespace std;
int map[M][M];
int dis[M][M];
int visite[M];
int kth_path(int s,int t,int k,int n)
{
	int i,j,v;
	dis[s][0]=0;
	dis[0][0]=inf; 
	while(1)
	{
		//cout<<"sf"<<endl;
		v=0;  
		for(i=1;i<=n;i++)
		   if(visite[i]<k&&dis[i][visite[i]]<dis[v][0])
		      v=i; 
		if(v==0)
	    	break; 
		if(v==t&&visite[t]==k-1) 
		    break;
		for(i=1;i<=n;i++)
		{
			if(visite[i]<k&&dis[v][visite[v]]+map[v][i]<dis[i][k]) 
			 {
					dis[i][k]=dis[v][visite[v]]+map[v][i]; 
					for(j=k;j>0;j--)
					if(dis[i][j]<dis[i][j-1])
					 {
							int temp=dis[i][j];
							dis[i][j]=dis[i][j-1];
							dis[i][j-1]=temp; 
					 }  
			 } 
		} 
		visite[v]++; 
	} 
	if(dis[t][k-1]<inf)
	  return dis[t][k-1];
	return -1; 
	 
} 
int main()
{
	int n,m;
	int a,b,c;
	int s,t,k; 
	int i,j; 
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
	{
	   visite[i]=0; 
	   for(j=1;j<=M;j++)
	   {
		 map[i][j]=inf; 
		 dis[i][j]=inf;
	   }
	 } 
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		if(map[a][b]>c)
			map[a][b]=c; 
	} 
	scanf("%d%d%d",&s,&t,&k);
	if(s==t)
	  ++k; 
	printf("%d\n",kth_path(s,t,k,n));
	//system("pause"); 
	return 0; 
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator