ZOJ 1655 Transport Goods

我没有深刻理解Dijkstra,,我检讨。。。我有错。。。。

此题是Dijkstra的变形,需要深刻理解Dijkstra的思想。。。。

由于缺少了Set[i] = true;导致不下20次的WA...

人家的好成绩都给我刷下来了。。。。

 

#include <iostream> #include <list> using namespace std; #define NUMOFPOINT 105 #define INFINITE -1 void Dijkstra(double Graph[][NUMOFPOINT], double dist[], int s, int n) { int i; list<int> L; list<int>::iterator it,next; int max; bool Set[NUMOFPOINT]; memset(Set, 0, sizeof(Set)); for (i=1;i<=n;i++) { dist[i] = Graph[s][i]; if (dist[i] != INFINITE) { L.push_front(i); //因为这里错,55555。我花了一个晚上呀!!!! //看来还是没有深刻理解Dijkstra.... //继续学习..... Set[i] = true; } } Set[s] = true; dist[s] = 1; while (!L.empty()) { it = next = L.begin(); max = *it; next++; while (next != L.end()) { if (dist[max]<dist[*next]) { max = *next; it = next; } next++; } L.erase(it); for (i=1;i<=n;i++) { if (Graph[max][i]!=INFINITE&&(Set[i]==false||dist[i]<Graph[max][i]*dist[max])) { dist[i] = Graph[max][i]*dist[max]; if (Set[i] == false) L.push_front(i); Set[i] = true; } } } } int main() { double Graph[NUMOFPOINT][NUMOFPOINT]; double dist[NUMOFPOINT]; double w[NUMOFPOINT]; int n,m; int a,b; double rate; int i,j; double sum; while (scanf("%d%d", &n, &m) != EOF) { for (i=0;i<=n;i++) for (j=0;j<=n;j++) Graph[i][j] = INFINITE; memset(dist, 0, sizeof(dist)); for (i=1;i<=n-1;i++) scanf("%lf", &w[i]); for (i=0;i<m;i++) { scanf("%d%d%lf", &a, &b, &rate); if (Graph[a][b]<1-rate) { Graph[a][b] = 1-rate; Graph[b][a] = 1-rate; } } Dijkstra(Graph, dist, n, n); sum = 0; for (i=1;i<n;i++) if (dist[i] != INFINITE) sum+=dist[i]*w[i]; printf("%.2f/n", sum); } return 0; }

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