Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Dij+矩阵倒置代码,献丑了~ 4500K,96MS#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator