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

我的dijk算法,请多指教呵呵

Posted by Icanmey at 2008-11-15 09:51:42 on Problem 1062
#include<iostream>
#include<memory>
using namespace std;
#define max 1000000000
long c[105][105];
long dist[105];
long dis(int n,int m,long *dj)
{

	int i,j,k;
	long min=max;
	for(k=0;k<=m;k++)
	{
		int u;
		bool * s = new bool [n+1];
		for(i=1;i<=n;i++)
		{
			if(dj[1]-m+k>dj[i]||dj[1]+k<dj[i])
			{
				dist[i]=max;
				s[i]=true;
			}
			else
			{
				dist[i]=c[0][i];
				s[i]=false;
			}
		}
		for(i=1;i<=n;i++)
		{
			long temp=max;
			u=1;
			for(j=1;j<=n;j++)
				if(!s[j]&&dist[j]<temp)
				{
					u=j;
					temp=dist[j];
				}
			s[u]=true;
			for(j=1;j<=n;j++)
				if(!s[j]&&c[u][j]<max)
				{
					long newdist=dist[u]+c[u][j];
					if(newdist<dist[j])
						dist[j]=newdist;
				}
		}
		delete [] s;
		if(min>dist[1])
			min=dist[1];
	}
	return min;
}
int main()
{
	int m,n;
	cin>>m>>n;
	long* dj = new long [n+1];
	int i,j;
	for(i=0;i<=n;i++)
		for(j=0;j<=n;j++)
			c[i][j]=max;
	for(i=1;i<=n;i++)
	{
		int p,k;
		cin>>p>>dj[i]>>k;
		c[0][i]=p;
		for(j=1;j<=k;j++)
		{
			int temp1;
			long temp2;
			cin>>temp1>>temp2;
			c[temp1][i]=temp2;
		}
	}
	long min=dis(n,m,dj);
	cout<<min<<endl;
	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