FOJ 1436 Renting Boats

用了有点变态的Dijkstra嘿嘿。。。

自己改造的。。。

 

#include <stdio.h> #include <string.h> #define MAX 205 #define INFINITE 99999 int Dijkstra(int Graph[][MAX], int n) { int Set[MAX]; int dist[MAX]; memset(Set, 0, sizeof(Set)); int i,j,k; int min; for (i=1;i<=n;i++) { dist[i] = Graph[1][i]; if (dist[i] != INFINITE) Set[i] = 1; } for (j=2;j<=n;j++) { min = INFINITE; for (i=2;i<=n;i++) if (Set[i]==1&&dist[i]<min) { min = dist[i]; k = i; } Set[k] = 2; for (i=2;i<=n;i++) if (Graph[k][i]!=INFINITE&&(Set[i]==0||dist[i]>Graph[k][i]+dist[k])) { dist[i] = dist[k]+Graph[k][i]; if (Set[i] == 0) Set[i] = 1; } } return dist[n]; } int main() { int n; int i,j; int Graph[MAX][MAX]; while(scanf("%d", &n)!=EOF) { for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (i==j) Graph[i][j] = 0; else Graph[i][j] = INFINITE; for (i=1;i<n;i++) for (j=i+1;j<=n;j++) scanf("%d", &Graph[i][j]); printf("%d/n", Dijkstra(Graph, n)); } return 0; } 

 

 

 

你可能感兴趣的:(Graph,ini)