#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define MAX 1001 #define IFNIT (9223372036854775808) unsigned long long int pt[MAX][2] = { 0 }; int ptNum = 0; /*Kruskal start*/ typedef struct Edge { int p1; int p2; unsigned long long int length; }Edge; int parent[MAX] = { 0 }; void qsort(Edge a[], int begin, int end) { if (begin >= end)return; int l = begin, r = end; Edge k = a[l]; while (l < r) { while ((l < r) && (a[r].length >= k.length))r--; a[l] = a[r]; while ((l < r) && (a[l].length <= k.length))l++; a[r] = a[l]; } a[l] = k; qsort(a, begin, l - 1); qsort(a, l + 1, end); } int find(int x) { if (parent[x] == x) { return x; } else { parent[x] = find(parent[x]); return parent[x]; } } Edge edge[MAX*MAX/2] = {0}; unsigned long long int Kruskal() { int cur = 0; for (int i = 1; i <= ptNum; i++) { for (int j = i + 1; j <= ptNum; j++) { edge[cur].p1 = i; edge[cur].p2 = j; unsigned long long int offsetY = pt[i][0] - pt[j][0]; unsigned long long int offsetX = pt[i][1] - pt[j][1]; edge[cur++].length = offsetY*offsetY + offsetX*offsetX; } parent[i] = i; } qsort(edge, 0, cur-1); unsigned long long int result = 0; int count = 0; for (int i = 0; i < cur; i++) { int a = find(edge[i].p1); int b = find(edge[i].p2); if (a != b) { result += edge[i].length; parent[a] = b; count++; } if (count == ptNum - 1)return result; } return 0; } /*Kruskal end*/ /*Prim start*/ unsigned long long int map[MAX][MAX] = { 0 }; unsigned long long int Prim() { int visit[MAX] = { 0 }; unsigned long long int minDis[MAX] = { 0 }; for (int i = 1; i <= ptNum; i++) { for (int j = i + 1; j <= ptNum; j++) { unsigned long long int offsetY = pt[i][0] - pt[j][0]; unsigned long long int offsetX = pt[i][1] - pt[j][1]; map[j][i] = map[i][j] = offsetY*offsetY + offsetX*offsetX; } } unsigned long long int result = 0; visit[1] = true; for (int i = 2; i <= ptNum; i++) { minDis[i] = map[1][i]; } for (int i = 1; i < ptNum; i++) { unsigned long long int min = IFNIT; int minI = 0; for (int j = 1; j <= ptNum; j++) { if (visit[j])continue; if (min > minDis[j]) { min = minDis[j]; minI = j; } } visit[minI] = true; result += min; for (int j = 1; j <= ptNum; j++) { if ((map[minI][j] < minDis[j]) && (!visit[j])) { minDis[j] = map[minI][j]; } } } return result; } /*Prim end*/ int main(void) { freopen("input.txt", "r", stdin); int test_num = 0; scanf("%d\n", &test_num); for (int test_case = 1; test_case <= test_num; test_case++) { for (int i = 0; i < MAX * 2; i++)pt[0][i] = 0; scanf("%d\n", &ptNum); for (int i = 1; i <= ptNum; i++) { scanf("%d ", &pt[i][0]); } scanf("\n"); for (int i = 1; i <= ptNum; i++) { scanf("%d ", &pt[i][1]); } double price = 0; scanf("\n"); scanf("%lf\n", &price); //unsigned long long int finalCost = price*Prim(); unsigned long long int finalCost = price*Kruskal(); printf("#%d %llu\n", test_case, finalCost); } return 0; }
#include <iostream> #include <algorithm> using namespace std; #define MAX 105 typedef struct _edge { int x; int y; int w; }edge; edge Edge[MAX]; int T; int N, E, Answer; int pre[MAX]; int cmp(const edge& A, const edge& B) { return A.w < B.w; } int find(int x) { return (pre[x] == x) ? x : pre[x] = find(pre[x]); } bool Union(int x, int y) { x = find(x); y = find(y); if(x == y) return false; else return pre[y] = x; } void kruskal() { for(int i = 0; i < N; i++) pre[i] = i; sort(Edge, Edge + E, cmp); for(int i = 0; i < E; i++) { if(Union(Edge[i].x, Edge[i].y) && N--) Answer += Edge[i].w; } for(int i = 0; i < T; i++) cout <<"pre["<<i<<"] = " << pre[i] << endl; if(N <= 1) cout << Answer << endl; else cout << "?" << endl; } int main() { while(cin >> N >> E, N) { T = N; Answer = 0; for(int i = 0; i < E; i++) { cin >> Edge[i].x >> Edge[i].y >> Edge[i].w; } kruskal(); } }
#if 0 #include <iostream> #include <iomanip> #include <cstdio> using namespace std; #define _DEBUG 0 #define MAX 1001 #define INF 0x7fffffff typedef struct _point { unsigned long long int x; unsigned long long int y; }point; point coord[1001]; int N; unsigned long long int graph[MAX][MAX]; unsigned long long int dist[MAX]; bool visited[MAX]; double tax; unsigned long long int Answer; void InitData() { for(int i = 0 ; i < MAX; i++) { for(int j = 0; j < MAX; j++) { graph[i][j] = INF; } coord[i].x = 0; coord[i].y = 0; dist[i] = INF; visited[i] = false; } } void _CalPath() { visited[1] = true; for(int i = 1; i <= N; i++) { if(visited[i] == false && dist[i] > graph[1][i]) dist[i] = graph[1][i]; } for(int i = 1; i < N; i++) { int min = INF, index; for(int j = 1; j <= N; j ++) { if(visited[j] == false && min > dist[j]) { min = dist[j]; index = j; } } #if _DEBUG cout << "index = " << index << endl; #endif visited[index] = true; Answer += min; for(int j = 1; j <= N; j++) { if(visited[j] == false && dist[j] > graph[index][j]) dist[j] = graph[index][j]; } } } unsigned long long Dis(int s, int e) { return (coord[s].x - coord[e].x) * (coord[s].x - coord[e].x) + (coord[s].y - coord[e].y) * (coord[s].y - coord[e].y); } int main() { int T, test_case; freopen("input_hanaro.txt", "r", stdin); cin >> T; for(test_case = 0; test_case < T; test_case++) { InitData(); Answer = 0; cin >> N; for(int i = 1; i <= 2 * N + 1; i++) { if(i == 2 * N + 1) cin >> tax; else if(i <= N) cin >> coord[i].x; else cin >> coord[i-N].y; } for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { if(graph[i][j] != INF) continue; graph[i][j] = graph[j][i] = Dis(i, j); } } #if _DEBUG for(int i = 1; i <= N; i++) { cout << i << " : " << coord[i].x << " " << coord[i].y << endl; } cout << "tax = " << tax << endl; for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { cout << graph[i][j] << " "; } cout << endl; } #endif _CalPath(); Answer *= tax; cout << "#" << test_case+1 << " " << setiosflags(ios::fixed) << Answer << endl; } } #endif #if 1 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define MAX 1001 #define IFNIT (9223372036854775808) unsigned long long int pt[MAX][2] = { 0 }; int ptNum = 0; /*Kruskal start*/ typedef struct Edge { int p1; int p2; unsigned long long int length; }Edge; int parent[MAX] = { 0 }; void qsort(Edge a[], int begin, int end) { if (begin >= end)return; int l = begin, r = end; Edge k = a[l]; while (l < r) { while ((l < r) && (a[r].length >= k.length))r--; a[l] = a[r]; while ((l < r) && (a[l].length <= k.length))l++; a[r] = a[l]; } a[l] = k; qsort(a, begin, l - 1); qsort(a, l + 1, end); } int find(int x) { if (parent[x] == x) { return x; } else { parent[x] = find(parent[x]); return parent[x]; } } Edge edge[MAX*MAX/2] = {0}; unsigned long long int Kruskal() { int cur = 0; for (int i = 1; i <= ptNum; i++) { for (int j = i + 1; j <= ptNum; j++) { edge[cur].p1 = i; edge[cur].p2 = j; unsigned long long int offsetY = pt[i][0] - pt[j][0]; unsigned long long int offsetX = pt[i][1] - pt[j][1]; edge[cur++].length = offsetY*offsetY + offsetX*offsetX; } parent[i] = i; } qsort(edge, 0, cur-1); unsigned long long int result = 0; int count = 0; for (int i = 0; i < cur; i++) { int a = find(edge[i].p1); int b = find(edge[i].p2); if (a != b) { result += edge[i].length; parent[a] = b; count++; } if (count == ptNum - 1)return result; } return 0; } /*Kruskal end*/ /*Prim start*/ unsigned long long int map[MAX][MAX] = { 0 }; unsigned long long int Prim() { int visit[MAX] = { 0 }; unsigned long long int minDis[MAX] = { 0 }; for (int i = 1; i <= ptNum; i++) { for (int j = i + 1; j <= ptNum; j++) { unsigned long long int offsetY = pt[i][0] - pt[j][0]; unsigned long long int offsetX = pt[i][1] - pt[j][1]; map[j][i] = map[i][j] = offsetY*offsetY + offsetX*offsetX; } } unsigned long long int result = 0; visit[1] = true; for (int i = 2; i <= ptNum; i++) { minDis[i] = map[1][i]; } for (int i = 1; i < ptNum; i++) { unsigned long long int min = IFNIT; int minI = 0; for (int j = 1; j <= ptNum; j++) { if (visit[j])continue; if (min > minDis[j]) { min = minDis[j]; minI = j; } } visit[minI] = true; result += min; for (int j = 1; j <= ptNum; j++) { if ((map[minI][j] < minDis[j]) && (!visit[j])) { minDis[j] = map[minI][j]; } } } return result; } /*Prim end*/ int main(void) { freopen("input_hanaro.txt", "r", stdin); int test_num = 0; scanf("%d\n", &test_num); for (int test_case = 1; test_case <= test_num; test_case++) { for (int i = 0; i < MAX * 2; i++)pt[0][i] = 0; scanf("%d\n", &ptNum); for (int i = 1; i <= ptNum; i++) { scanf("%d ", &pt[i][0]); } scanf("\n"); for (int i = 1; i <= ptNum; i++) { scanf("%d ", &pt[i][1]); } double price = 0; scanf("\n"); scanf("%lf\n", &price); //unsigned long long int finalCost = price*Prim(); unsigned long long int finalCost = price*Kruskal(); printf("#%d %llu\n", test_case, finalCost); } return 0; } #endif
输入
20 2 0 0 0 100 1.0 4 0 0 400 400 0 100 0 100 1.0 6 0 0 400 400 1000 2000 0 100 0 100 600 2000 0.3 8 0 155 92 400 566 1256 964 3000 0 166 1000 10 236 778 1000 3000 0.2 9 567 5 45674 24 797 29 0 0 0 345352 5464 145346 54764 5875 0 3453 4545 123 0.0005 10 798 915 797 463 895 523 959 702 235 523 126 25 402 45 841 762 982 605 616 78 0.66
1024
时间: 2024-10-29 10:46:23