第一次用dijkstra做题,思路很清晰,但是写完发现竟然有130行...调试都调了好一会
看了看别人同是用dijkstra做,不到50行,压力山大,看来以后做对了也得看看别人的题解
1 /* 2 3 ID: hubiao cave 4 5 PROG: comehome 6 7 LANG: C++ 8 9 */ 10 11 12 13 14 #include<iostream> 15 16 #include<fstream> 17 18 #include<string> 19 #include<set> 20 21 #define MAX 999999; 22 23 24 using namespace std; 25 26 27 28 int path[60][60]; 29 int s[60]; 30 int u[60]; 31 bool isCow[60]; 32 33 void dijkstra(); 34 int extract_min(int*); 35 void relax(int); 36 bool isEmpty(); 37 int main() 38 39 { 40 41 ifstream fin("comehome.in"); 42 43 ofstream fout("comehome.out"); 44 45 int num; 46 char ch1,ch2; 47 int value; 48 fin>>num; 49 for(int i=0;i<num;++i) 50 { 51 fin>>ch1>>ch2>>value; 52 53 if(path[ch1<'a'?ch1-'A'+1:ch1-'a'+1+26][ch2<'a'?ch2-'A'+1:ch2-'a'+1+26]>value||path[ch1<'a'?ch1-'A'+1:ch1-'a'+1+26][ch2<'a'?ch2-'A'+1:ch2-'a'+1+26]==0) 54 { 55 56 path[ch1<'a'?ch1-'A'+1:ch1-'a'+1+26][ch2<'a'?ch2-'A'+1:ch2-'a'+1+26]=value; 57 path[ch2<'a'?ch2-'A'+1:ch2-'a'+1+26][ch1<'a'?ch1-'A'+1:ch1-'a'+1+26]=value; 58 s[ch1<'a'?ch1-'A'+1:ch1-'a'+1+26]=MAX; 59 s[ch2<'a'?ch2-'A'+1:ch2-'a'+1+26]=MAX; 60 if(ch1<'a') 61 isCow[ch1-'A'+1]=true; 62 if(ch2<'a') 63 isCow[ch2-'A'+1]=true; 64 } 65 66 67 } 68 s['Z'-'A'+1]=0; 69 dijkstra(); 70 71 int minindex=-1; 72 for(int i=1;i<=25;i++) 73 { 74 if(isCow[i]&&minindex==-1) 75 minindex=i; 76 else if(isCow[i]&&u[i]<u[minindex]) 77 minindex=i; 78 } 79 80 fout<<static_cast<char>(minindex+'A'-1)<<" "<<u[minindex]<<endl; 81 82 83 84 85 86 return 0; 87 88 89 } 90 void dijkstra() 91 { 92 int renode; 93 while(!isEmpty()) 94 { 95 renode=extract_min(s); 96 if(renode==1) 97 renode=1; 98 relax(renode); 99 u[renode]=s[renode]; 100 s[renode]=-1; 101 if(renode==26) 102 u[renode]=-1; 103 104 } 105 } 106 107 bool isEmpty() 108 { 109 for(int i=1;i<=52;i++) 110 { 111 if(s[i]>0&&i!=26) 112 return false; 113 } 114 return true; 115 } 116 117 void relax(int renode) 118 { 119 for(int i=1;i<=52;i++) 120 { 121 if(s[i]!=-1&&i!=renode&&path[renode][i]&&s[i]>s[renode]+path[renode][i]) 122 s[i]=s[renode]+path[renode][i]; 123 } 124 } 125 int extract_min(int*s) 126 { 127 int minindex=1; 128 for(int i=1;i<=52;i++) 129 { 130 if(i!=26&&s[i]>0) 131 { 132 if(s[minindex]==0||s[minindex]==-1) 133 minindex=i; 134 else if(s[minindex]>s[i]) 135 minindex=i; 136 else 137 continue; 138 139 } 140 else if(i==26) 141 { 142 if(s[i]==0) 143 return i; 144 } 145 } 146 147 return minindex; 148 }
贴个别人的...
1 2 #include <algorithm> 3 #include <climits> 4 #include <cstdio> 5 using namespace std; 6 7 bool flag[52] = {}; 8 int a[52][52], dist[52]; 9 10 int main() 11 { 12 int p; 13 freopen("comehome.in", "r", stdin); 14 freopen("comehome.out", "w", stdout); 15 for (int i = 0; i < 52; ++i) 16 fill_n(a[i], 52, INT_MAX/2); 17 for (scanf("%d", &p); p; --p) 18 { 19 char u, v; 20 int w; 21 scanf("%*c%c %c%d", &u, &v, &w); 22 u = islower(u) ? u-'a' : u-'A'+26; 23 v = islower(v) ? v-'a' : v-'A'+26; 24 if (w < a[u][v]) 25 a[u][v] = w, a[v][u] = w; 26 } 27 copy(a[51], a[51]+52, dist); 28 for(;;) 29 { 30 int min_dist = INT_MAX, u; 31 for (int i = 0; i < 51; ++i) 32 if (!flag[i] && dist[i] < min_dist) 33 min_dist = dist[i], u = i; 34 if (u >= 26) return printf("%c %d ", u-26+'A', min_dist), 0; 35 flag[u] = true; 36 for (int i = 0; i < 51; ++i) 37 if (!flag[i] && dist[u]+a[u][i] < dist[i]) 38 dist[i] = dist[u]+a[u][i]; 39 } 40 }