/主要是图算法方面的, BFS. DFS. 生成树. 最短路径. 二分图. 最大流. 差分约束之类。
1 //直接BFS即可。 注意n>k的情况 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <cstdlib> 12 13 using namespace std; 14 15 int n, k; 16 17 int main() 18 { 19 int ans = 1000000000; 20 while (~scanf("%d%d", &n, &k)) 21 { 22 ans = abs(n-k); 23 if (n >= k) 24 { 25 printf("%d ", ans); 26 continue; 27 } 28 queue<pair<int, int> > qu; 29 while (!qu.empty()) qu.pop(); 30 set<int> st; 31 qu.push(make_pair(n, 0)); 32 st.insert(n); 33 while (!qu.empty()) 34 { 35 int fir = qu.front().first; 36 int sec = qu.front().second; 37 if (ans <= sec) break; 38 if (fir == k) 39 { 40 ans = sec; 41 break; 42 } 43 else if (fir < k) 44 { 45 if (st.count(fir-1) == 0) qu.push(make_pair(fir-1, sec+1)), st.insertfir-1); 46 if (st.count(fir*2) == 0) qu.push(make_pair(2*fir, sec+1)), st.insert(2*fir); 47 if (st.count(fir+1) == 0) qu.push(make_pair(fir+1, sec+1)), st.insert(fir+1); 48 } 49 else ans = min(fir-k+sec, ans); 50 qu.pop(); 51 } 52 printf("%d ", ans); 53 } 54 55 return 0; 56 }
1 //Left && Right分着写的,懒得再合起来了。BFS即可。 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <cstdlib> 12 13 using namespace std; 14 15 const int maxn = 45; 16 char g[maxn][maxn]; 17 int vis[maxn][maxn]; 18 int t, m, n; 19 int sx, sy, ex, ey; 20 21 void init() 22 { 23 for (int i = 0 ; i < maxn; ++i) 24 for (int j = 0; j < maxn; ++j) 25 g[i][j] = '#'; 26 } 27 28 int workLeft() 29 { 30 int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; 31 int p = 0, x = sx, y = sy, d = 1; 32 if (sx == 1) p = 0; 33 else if (sy == n) p = 1; 34 else if (sx == m) p = 2; 35 else p = 3; 36 while (x != ex || y != ey) 37 { 38 d++; 39 for (int i = 0; i < 4; ++i) 40 { 41 int xx = x + dir[(n+4-1)%4][0]; 42 int yy = y + dir[(n+4-1)%4][1]; 43 if (g[xx][yy] != '#') 44 { 45 x = xx, y = yy, n = (n+4-1)%4; 46 break; 47 } 48 else n = (n+1)%4; 49 } 50 } 51 return d; 52 } 53 54 int workRight() 55 { 56 int dir[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; 57 int x = sx, y = sy, d = 1; 58 int p = 0; 59 if (sx == 0) p = 0; 60 else if (sy == 1) p = 1; 61 else if (sx == m) p = 2; 62 else p = 3; 63 while (x != ex || y != ey) 64 { 65 d++; 66 for (int i = 0; i < 4; ++i) 67 { 68 int xx = x + dir[(n+4-1)%4][0]; 69 int yy = y + dir[(n+4-1)%4][1]; 70 if (g[xx][yy] != '#') 71 { 72 x = xx, y = yy, n = (n+4-1)%4; 73 break; 74 } 75 else n = (n+1)%4; 76 } 77 } 78 return d; 79 } 80 81 int workBest() 82 { 83 int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; 84 memset(vis, 0, sizeof(vis)); 85 queue<pair<pair<int, int>, int> > qu; 86 qu.push(make_pair(make_pair(sx, sy), 1)); 87 vis[sx][sy] = 1; 88 while (!qu.empty()) 89 { 90 int x = qu.front().first.first; 91 int y = qu.front().first.second; 92 int d = qu.front().second; 93 qu.pop(); 94 if (x == ex && y == ey) return d; 95 for (int i = 0; i < 4; ++i) 96 { 97 int xx = x + dir[i][0]; 98 int yy = y + dir[i][1]; 99 if (!vis[xx][yy] && g[xx][yy] != '#') 100 { 101 qu.push(make_pair(make_pair(xx, yy), d+1)); 102 vis[xx][yy] = 1; 103 } 104 } 105 } 106 } 107 108 int main() 109 { 110 scanf("%d", &t); 111 while (t--) 112 { 113 init(); 114 scanf("%d%d", &n, &m); 115 for (int i = 1; i <= m; ++i) 116 { 117 getchar(); 118 for (int j = 1; j <= n; ++j) 119 { 120 scanf("%c", &g[i][j]); 121 if (g[i][j] == 'S') 122 sx = i, sy = j; 123 if (g[i][j] == 'E') 124 ex = i, ey = j; 125 } 126 } 127 printf("%d %d %d ", workLeft(), workRight(), workBest()); 128 } 129 130 return 0; 131 }
1 //Bellman-Ford的变形 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <cstdlib> 12 13 using namespace std; 14 15 const int maxn = 105; 16 17 class Node 18 { 19 public: 20 int a, b; 21 double r, c; 22 Node(int aa, int bb, double rr, double cc): a(aa), b(bb), r(rr), c(cc){} 23 }; 24 25 vector<Node> vec; 26 double dis[maxn]; 27 double v; 28 int n, m, s; 29 30 void bellmanFord() 31 { 32 memset(dis, 0, sizeof(dis)); 33 dis[s] = v; 34 for (int i = 1; i <= n-1; ++i) 35 { 36 for (int j = 0; j < vec.size(); ++j) 37 dis[vec[j].b] = max(dis[vec[j].b] ,(dis[vec[j].a]-vec[j].c)*vec[j].r); 38 } 39 40 for (int i = 0; i < vec.size(); ++i) 41 { 42 if (dis[vec[i].b] < (dis[vec[i].a]-vec[i].c)*vec[i].r) 43 { 44 printf("YES "); 45 return ; 46 } 47 } 48 puts("NO"); 49 } 50 51 int main() 52 { 53 while (~scanf("%d%d%d%lf", &n, &m, &s, &v)) 54 { 55 vec.clear(); 56 int a, b; 57 double r1, c1, r2, c2; 58 for (int i = 0; i < m; ++i) 59 { 60 scanf("%d%d%lf%lf%lf%lf", &a, &b, &r1, &c1, &r2, &c2); 61 Node node1(a, b, r1, c1), node2(b, a, r2, c2); 62 vec.push_back(node1), vec.push_back(node2); 63 } 64 bellmanFord(); 65 } 66 67 return 0; 68 }
1 //依旧是BellmanFord即可,不过题目数据太差了,n的数据量不是500,所以开大dis数组即可。 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <cstdlib> 12 13 using namespace std; 14 15 #define INF 1e9 16 const int maxn = 10015; 17 18 class Lode 19 { 20 public: 21 int s, e, t; 22 Node(int ss, int ee, int tt): s(ss), e(ee), t(tt){} 23 }; 24 vector<Node> vec; 25 int f, n, m, w; 26 int dis[maxn]; 27 28 void init() 29 { 30 vec.clear(); 31 for (int i = 0; i < maxn; ++i) 32 dis[i] = INF; 33 } 34 35 void bellmanFord() 36 { 37 dis[1] = 0; 38 for (int i = 1; i < n-1; ++i) 39 { 40 for (int j = 0; j < vec.size(); ++j) 41 dis[vec[j].e] = min(dis[vec[j].e], dis[vec[j].s]+vec[j].t); 42 } 43 for (int i = 0; i < vec.size(); ++i) 44 { 45 if (dis[vec[i].e] > dis[vec[i].s] + vec[i].t) 46 { 47 puts("YES"); 48 return ; 49 } 50 } 51 puts("NO"); 52 } 53 54 int main() 55 { 56 scanf("%d", &f); 57 while (f--) 58 { 59 init(); 60 scanf("%d%d%d", &n, &m, &w); 61 int s, e, t; 62 for(int i = 0; i < m; ++i) 63 { 64 scanf("%d%d%d", &s, &e, &t); 65 Node node1(s, e, t); 66 vec.push_back(node1); 67 Node node2(e, s, t); 68 vec.push_back(node2); 69 } 70 for (int i = 0; i < w; ++i) 71 { 72 scanf("%d%d%d", &s, &e, &t); 73 Node node(s, e, -t); 74 vec.push_back(node); 75 } 76 bellmanFord(); 77 } 78 79 return 0; 80 }
5.昂贵的聘礼
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <cstdlib> 11 12 using namespace std; 13 14 #define INF 1e9 15 const int maxn = 105; 16 17 int price[maxn], g[maxn][maxn], level[maxn]; 18 int dis[maxn], vis[maxn]; 19 int n, m, ans; 20 21 void init() 22 { 23 memset(price, 0, sizeof(price)); 24 memset(level, 0, sizeof(level)); 25 for (int i = 0; i <= n; ++i) 26 for (int j = 0; j <= n; ++j) 27 g[i][j] = INF; 28 } 29 30 int dijkstra() 31 { 32 for (int i = 1; i <= n; ++i) 33 dis[i] = price[i]; 34 for (int i = 1; i <= n; ++i) 35 { 36 int temp = INF, x; 37 for (int j = 1; j <= n; ++j) 38 if (!vis[j] && dis[j] < temp) 39 temp = dis[x = j]; 40 vis[x] = 1; 41 for (int j = 1; j <= n; ++j) 42 if (dis[x]+g[x][j] < dis[j] && !vis[j]) 43 dis[j] = dis[x]+g[x][j]; 44 } 45 return dis[1]; 46 } 47 48 int main() 49 { 50 while (~scanf("%d%d", &m, &n)) 51 { 52 init(); 53 for (int i = 1; i <= n; ++i) 54 { 55 int x; 56 scanf("%d%d%d", &price[i], &level[i], &x); 57 for (int j = 0; j < x; ++j) 58 { 59 int t, v; 60 scanf("%d%d", &t, &v); 61 g[t][i] = v; 62 } 63 g[0][i] = price[i]; 64 } 65 ans = INF; 66 for (int i = 1; i <= n; ++i) 67 { 68 int minl = level[i]; 69 for<span> (int j = 1; j <= n; ++j) 70 { 71 if (level[j]-minl > m || minl > level[j]) vis[j] = 1; 72 else vis[j] = 0; 73 } 74 int now = dijkstra(); 75 ans = min(now, ans); 76 } 77 printf("%d ", ans); 78 } 79 80 return 0; 81 }
1 //差分约束系统,差分约数系统的含义,其实就是如果有n个变量在m个形如aj-ai>=bk条件下,求解的此不等式的方法。而这种不等式的解法其实就是转化为图论的最小路的算法求解的。 2 根据题意可得不等式//Sbi - Sai >= ci//Si - Si-1 >= 0//Si-1 - Si >= -1 3 #include <iostream> 4 #include <string> 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 #include <queue> 9/span> #include <vector> 10 #include <set> 11 #include <map> 12 #include <cstdlib> 13 14 using namespace std; 15 16 #define INF 1000000007 17 #define MIN(a, b) (a > b ? b : a) 18 #define MAX(a, b) (a > b ? a : b) 19 #define MAXN 50005 20 const int maxn = 50005; 21 struct Edge 22 { 23 int v; 24 int w; 25 int next; 26 }edge[MAXN*3]; 27 int head[MAXN], d[MAXN], vis[MAXN]; 28 int n, t, source, target; 29 30 void init() 31 { 32 memset(edge, 0, sizeof(edge)); 33 memset(vis, 0, sizeof(vis)); 34 memset(head, -1, sizeof(head)); 35 t = 0; 36 source = INF, target = -INF; 37 } 38 39 void addEdge(int u, int v, int w) 40 { 41 edge[t].v = v; 42 edge[t].w = w; 43 edge[t].next = head[u]; 44 head[u] = t++; 45 } 46 47 void spfa() 48 { 49 for (int i = source; i <= target; ++i) d[i] = -INF; 50 d[source] = 0; 51 queue<int> qu; 52 qu.push(source); 53 while (!qu.empty()) 54 { 55 int x = qu.front(); 56 qu.pop(); 57 vis[x] = 0; 58 for (int e = head[x]; e != -1; e = edge[e].next) 59 if (d[edge[e].v] < d[x]+edge[e].w) 60 { 61 d[edge[e].v] = d[x] + edge[e].w; 62 if (!vis[edge[e].v]) 63 { 64 qu.push(edge[e].v); 65 vis[edge[e].v] = 1; 66 } 67 } 68 } 69 } 70 71 int main() 72 { 73 int u, v, w; 74 init(); 75 scanf("%d", &n); 76 for (int i = 0; i < n; ++i) 77 { 78 scanf("%d%d%d", &u, &v, &w); 79 addEdge(u, v+1, w); 80 source = min(u, source); 81 target = max(v+1, target); 82 } 83 for (int i = source; i < target; ++i) 84 { 85 addEdge(i, i+1, 0); 86 addEdge(i+1, i, -1); 87 } 88 spfa(); 89 printf("%d ", d[target]); 90 91 return 0; 92 }
1 //差分约束,脑残了,一开始一直拿着带输出测试数据的代码提交,一直wa。。。隐含条件Aj-Ai=无穷大,Ai-Aj<=0 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <set> 10 #include <map> 11 #include <cstdlib> 12 13 using namespace std; 14 15 #define INF 1000000007 16 #define MIN(a, b) (a > b ? b : a) 17 #define MAX(a, b) (a > b ? a : b) 18 #define MAXN 1005 19 #define maxn 10005 20 struct Edge 21 { 22 int v; 23 int w; 24 int next; 25 }edge[maxn*3]; 26 int vis[MAXN], d[MAXN], head[MAXN], use[MAXN]; 27 int t, T, n; 28 29 void init() 30 { 31 t = 0; 32 memset(edge, 0, sizeof(edge)); 33 for (int i = 0; i < MAXN; ++i) 34 use[i] = 0, vis[i] = 0, d[i] = INF, head[i] = -1; 35 } 36 37 void addEdge(int u, int v, int w) 38 { 39 edge[t].v = v; 40 edge[t].w = w; 41 edge[t].next = head[u]; 42 head[u] = t++; 43 } 44 45 int spfa() 46 { 47 queue<int> qu; 48 while (!qu.empty()) qu.pop(); 49 d[1] = 0; 50 qu.push(1); 51 while (!qu.empty()) 52 { 53 int x = qu.front(); 54 qu.pop(); 55 vis[x] = 0; 56 for (int e = head[x]; e != -1; e = edge[e].next) 57 { 58 if (d[edge[e].v] > d[x]+edge[e].w) 59 { 60 d[edge[e].v] = d[x]+edge[e].w; 61 if (!vis[edge[e].v]) 62 { 63 qu.push(edge[e].v); 64 vis[edge[e].v] = 1; 65 if (++use[edge[e].v] > n) return -1; 66 } 67 } 68 } 69 } 70 if (d[n] == INF) return -2; 71 return d[n]; 72 } 73 74 int main() 75 { 76 scanf("%d", &T); 77 while (T--) 78 { 79 init(); 80 int x, y; 81 scanf("%d%d%d", &n, &x, &y); 82 int a, b, c; 83 for (int i = 0; i < x; ++i) 84 { 85 scanf("%d%d%d", &a, &b, &c); 86 addEdge(a, b, c); 87 } 88 for (int i = 0; i < y; ++i) 89 { 90 scanf("%d%d%d", &a, &b, &c); 91 addEdge(b, a, -c); 92 } 93 for (int i = 2; i <= n; ++i) 94 { 95 addEdge(i-1, i, INF); 96 addEdge(i, i-1, 0); 97 } 98 printf("%d ", spfa()); 99 } 100 101 return 0; 102 }
1 //差分约束,构建不等式然后选取最长路。我觉得这题不严谨。。比如FAF 1 2 2 1,我觉得应该是impossible...但是。。。 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <string> 6 #include <cstring> 7 #include <set> 8 #include <stdint.h> 9 #include <utility> 10 #include <map> 11 #include <cstdlib> 12 #include <queue> 13 #include <vector> 14 #include <numeric> 15 #include <list> 16 #include <bitset> 17 #include <exception> 18 #include <istream> 19 #include <ostream> 20 #include <stdexcept> 21 #include <functional> 22 #include <stack> 23 #include <unordered_set> 24 25 #define LL long long int 26 using namespace std; 27 28 29 #define MEM memset 30 #define MAX (a > b ? a : b) 31 #define MIN (a < b ? a : b) 32 #define MAXN 200000 33 #define LL long long int 34 #define INF 1000000007 35 36 struct Edge 37 { 38 int v; 39 int w; 40 int next; 41 }edge[MAXN]; 42 int d[MAXN], vis[MAXN], use[MAXN], weight[MAXN], head[MAXN], in[MAXN]; 43 int ncas, n, t, a1, a2; 44 char ch[10]; 45 46 void init() 47 { 48 t = 0; 49 memset(edge, 0, sizeof(edge)); 50 for (int i = 0; i < MAXN; ++i) 51 vis[i] = weight[i] = use[i] = in[i] = 0, d[i] = -INF, head[i] = -1; 52 } 53 54 void addEdge(int u, int v, int w) 55 { 56 edge[t].v = v; 57 edge[t].w = w; 58 edge[t].next = head[u]; 59 head[u] = t++; 60 } 61 62 void judge() 63 { 64 in[a1]++; 65 if (strcmp(ch,"SAF") == 0) addEdge(a2, a1, weight[a2]); 66 else if (strcmp(ch, "FAF") == 0) addEdge(a2, a1, weight[a2]-weight[a1]); 67 else if (strcmp(ch, "FAS") == 0) addEdge(a2, a1, -weight[a1]); 68 else addEdge(a2, a1, 0); 69 } 70 71 int spfa() 72 { 73 int s = 0; 74 queue<int> qu; 75 while (!qu.empty()) qu.pop(); 76 d[s] = 0; 77 qu.push(s); 78 vis[s] = 1; 79 while (!qu.empty()) 80 { 81 int x = qu.front(); 82 qu.pop(); 83 vis[x] = 0; 84 use[x]++; 85 if (use[x] > n) return 1; 86 for (int e = head[x]; e != -1; e = edge[e].next) 87 { 88 if (d[edge[e].v] < d[x]+edge[e].w) 89 { 90 d[edge[e].v] = d[x]+edge[e].w; 91 if (!vis[edge[e].v]) 92 { 93 qu.push(edge[e].v); 94 vis[edge[e].v] = 1; 95 } 96 } 97 } 98 } 99 for (int i = 1; i <= n; ++i) 100 printf("%d %d ", i, d[i]); 101 return 0; 102 } 103 104 int main() 105 { 106 ncas = 1; 107 while (~scanf("%d", &n) && n) 108 { 109 init(); 110 for (int i = 1; i <= n; ++i) 111 scanf("%d", &weight[i]); 112 while (scanf("%s", ch) && ch[0] != '#') 113 { 114 scanf("%d %d", &a1, &a2); 115 judge(); 116 } 117 for (int i = 1; i <= n; ++i) 118 addEdge(0, i, 0); 119 printf("Case %d: ", ncas++); 120 if (spfa()) puts("impossible"); 121 puts(""); 122 } 123 124 return 0; 125 }
1 //Prim算法 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <string> 6 #include <cstring> 7 #include <set> 8 #include <utility> 9 #include <map> 10 #include <cstdlib> 11 #include <queue> 12 #include <vector> 13 #include <numeric> 14 #include <list> 15 #include <bitset> 16 #include <exception> 17 #include <istream> 18 #include <ostream> 19 #include <qtdexcept> 20 #include <functional> 21 #include <stack> 22 23 #define LL long long int 24 using namespace std; 25 26 27 #define MEM memset 28 #define MAX (a > b ? a : b) 29 #define MIN (a < b ? a : b) 30 #define MAXN 2010 31 #define LL long long int 32 #define INF 1000000007 33 34 int g[MAXN][MAXN]; 35 int d[MAXN], vis[MAXN]; 36 char ch[MAXN][10]; 37 int n; 38 39 void init() 40 { 41 memset(vis, 0, sizeof(vis)); 42 for (int i = 0; i < n; ++i) 43 for (int j = 0; j < n; ++j) 44 { 45 if (i == j) g[i][j] = 0; 46 else 47 { 48 int cnt = 0; 49 for (int k = 0; k < 7; ++k) 50 if (ch[i][k] != ch[j][k]) cnt++; 51 g[i][j] = g[j][i] = cnt; 52 } 53 } 54 for (int i = 0; i < n; ++i) 55 d[i] = g[0][i]; 56 } 57 58 int prim() 59 { 60 init(); 61 int k = 0, idx, len, ans = 0; 62 vis[k] = 1; 63 for (int i = 1; i < n; ++i) 64 { 65 len = INF; 66 for (int j = 0; j < n; ++j) 67 if (!vis[j] && d[j] < len) 68 len = d[idx = j]; 69 ans += len; 70 vis[idx] = 1; 71 for (int j = 0; j < n; ++j) 72 if (!vis[j] && g[idx][j] < d[j]) 73 d[j] = g[idx][j]; 74 } 75 return ans; 76 } 77 78 int main() 79 { 80 while (~scanf("%d", &n) && n) 81 { 82 for (int i = 0; i < n; ++i) 83 scanf("%s", ch[i]); 84 printf("The highest possible quality is 1/%d. ", prim()); 85 } 86 87 return 0; 88 }
10.Highways
1 //Prim算法 2 #include <iostream> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <string> 7 #include <cstring> 8 9 using namespace std; 10 11 #define INF 1000000007 12 #define MAXN 505 13 #define MAX(a, b) (a > b ? a : b) 14 #define MIN(a, b) (a < b ? a : b) 15 16 int t, n; 17 int d[MAXN], vis[MAXN]; 18 int g[MAXN][MAXN]; 19 20 void init() 21 { 22 for (int i = 0; i < n; ++i) 23 d[i] = g[0][i], vis[i] = 0; 24 } 25 26 int prim() 27 { 28 init(); 29 int ans = -INF; 30 vis[0] = 1; 31 for (int i = 1; i < n; ++i) 32 { 33 int min_cost = INF, idx = 0; 34 for (int j = 0; j < n; ++j) 35 if (!vis[j] && d[j] < min_cost) 36 min_cost = d[idx = j]; 37 ans = max(ans, min_cost); 38 vis[idx] = 1; 39 for (int j = 0; j < n; ++j) 40 if (!vis[j] && d[j] > g[idx][j]) 41 d[j] = g[idx][j]; 42 } 43 return ans; 44 } 45 46 int main() 47 { 48 49 scanf("%d", &t); 50 while (t--) 51 { 52 scanf("%d", &n); 53 for (int i = 0; i < n; ++i) 54 for (int j = 0; j < n; ++j) 55 scanf("%d", &g[i][j]); 56 printf("%d ", prim()); 57 } 58 59 return 0; 60 }
11.Agri-Net
1 //prim算法 2 #include <iostream> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <string> 7 #include <cstring> 8 9 using namespace std; 10 11 #define INF 1000000007 12 #define MAXN 105 13 #define MAX(a, b) (a > b ? a : b) 14 #define MIN(a, b) (a < b ? a : b) 15 16 int n; 17 int d[MAXN], vis[MAXN]; 18 int g[MAXN][MAXN]; 19 20 void init() 21 { 22 for (int i = 0; i < n; ++i) 23 d[i] = g[0][i], vis[i] = 0; 24 } 25 26 int prim() 27 { 28 init(); 29 int ans = 0; 30 vis[0] = 1; 31 for (int i = 1; i < n; ++i) 32 { 33 int min_cost = INF, idx = 0; 34 for (int j = 0; j < n; ++j) 35 if (!vis[j] && d[j] < min_cost) 36 min_cost = d[idx = j]; 37 ans += min_cost; 38 vis[idx] = 1; 39 for (int j = 0; j < n; ++j) 40 if (!vis[j] && d[j] > g[idx][j]) 41 d[j] = g[idx][j]; 42 } 43 return ans; 44 } 45 46 int main() 47 { 48 while (~scanf("%d", $amp;n)) 49 { 50 for (int i = 0; i < n; ++i) 51 for (int j = 0; j < n; ++j) 52 scanf("%d", &g[i][j]); 53 printf("%d ", prim()); 54 } 55 56 return 0; 57 }
12.Agri-Net
1 //Kruskal算法 与上面prim对比一下 2 #include <iostream> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <string> 7 #include <cstring> 8 9 using namespace std; 10 11 #define INF 1000000007 12 #define MAXN 105 13 #define MAX(a, b) (a > b ? a : b) 14 #define MIN(a, b) (a < b ? a : b) 15 16 struct Edge 17 { 18 int u; 19 int v; 20 int w; 21 }edge[MAXN*MAXN]; 22 int n; 23 int father[MAXN]; 24 25 int cmp(const void* a, const void* b) 26 { 27 return ((Edge*)a)->w - ((Edge*)b)->w; 28 } 29 30 int findFather(int val) 31 { 32 if (father[val] == val) return val; 33 return father[val] = findFather(father[val]); 34 } 35 36 void unionFather(int x, int y) 37 { 38 int xx = findFather(x); 39 int yy = findFather(y); 40 father[xx] = father[yy]; 41 } 42 43 int main() 44 { 45 while (~scanf("%d", &n)) 46 { 47 for (int i = 0; i < n; ++i) 48 for (int j = 0; j < n; ++j) 49 { 50 scanf("%d", &edge[i*n+j].w); 51 edge[i*n+j].u = i, edge[i*n+j].v = j; 52 } 53 for (int i = 0; i < n; ++i) father[i] = i; 54 qsort(edge, n*n, sizeof(Edge), cmp); 55 int ans = 0; 56 for (int i = 0; i < n*n; ++i) 57 { 58 if (edge[i].u >= edge[i].v) continue; 59 if (findFather(edge[i].u) != findFather(edge[i].v)) 60 { 61 ans += edge[i].w; 62 unionFather(edge[i].u, edge[i].v); 63 } 64 } 65 printf("%d ", ans); 66 } 67 68 return 0; 69 }