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+矩阵倒置代码,献丑了~ 4500K,96MS

Posted by Veegin at 2010-08-04 00:54:24 on Problem 3268
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define N 1050
#define MaxInt 0x3f3f3f3f
int map[N][N],visited[N],dis[N],sum[N];
int n,m,x;

void Dijkstra()
{
	int i,j,position,min;
	memset(visited,0,sizeof(visited));
	for(i=1;i<=n;i++)
		dis[i]=map[x][i];
	visited[x]=1;
	for(i=1;i<n;i++)
	{
		min=MaxInt;
		for(j=1;j<=n;j++)
		{
			if(visited[j]==0&&min>dis[j])
			{
				min=dis[j];
				position=j;
			}
		}
		visited[position]=1;
		sum[position]+=min;
		for(j=1;j<=n;j++)
			if(visited[j]==0&&dis[position]+map[position][j]<dis[j])
				dis[j]=dis[position]+map[position][j];
	}
}

void Reversal()
{
	int i,j,temp;
	for(i=1;i<=n;i++)
		for(j=1;j<i;j++)
		{
			temp=map[i][j];
			map[i][j]=map[j][i];
			map[j][i]=temp;
		}
}

int main()
{
	int i,u,v,c,max=0;
	memset(map,MaxInt,sizeof(map));
	scanf("%d%d%d",&n,&m,&x);
	for(i=1;i<=n;i++)
		map[i][i]=0;
	while(m--)
	{
		scanf("%d%d%d",&u,&v,&c);
		if(c<map[u][v])
			map[u][v]=c;
	}
	Dijkstra();
	Reversal();
	Dijkstra();
	for(i=1;i<=n;i++)
		if(max<sum[i])
			max=sum[i];
	printf("%d\n",max); 
	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