#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <queue> #include <cfloat> #include <map> const int INF = 1000000; const int max_num = 1010; using namespace std; struct node { int w; int v; node(int a , int b) : v(a) , w(b) { }; }; int des[max_num] , pre[max_num]; vector<node> G[max_num]; bool visited[max_num]; int N; void Dijkstra(int s) { fill(des , des + N + 1 , INF); for (int i = 0; i < N; i++) pre[i] = i; memset(visited , 0 , N + 1); des[s] = 0; for (int i = 1 , Min , u; i <= N; i++) { Min = INF; u = -1; for (int k = 1; k <= N; k++) { if (!visited[k] && Min > des[k]) { Min = des[k]; u = k; } } if (u == -1) return; visited[u] = true; for (int k = 0; k < G[u].size(); k++) { int v = G[u][k].v; if(!visited[v]) { if (des[v] > des[u] + G[u][k].w) { des[v] = des[u] + G[u][k].w; pre[v] = u; } else if (des[v] == des[u] + G[u][k].w && pre[v] > u) pre[v] = u; } } } } void DFS(int s , int e) { if (s == e) { cout << e << " "; return; } DFS(s , pre[e]); cout << e << " "; } int main() { #ifdef _DEBUG freopen("data.txt", "r+", stdin); #endif // _DEBUG std::ios::sync_with_stdio(false); int M , s , t; while(cin >> N >> M >> s >> t) { for (int i = 1; i <= N; i++) G[i].clear(); for (int i = 0, d = 1; i < M; i++ , d *= 2) { int u , v , w; cin >> u >> v >> w; G[u].push_back(node(v , w)); G[v].push_back(node(u , w)); } Dijkstra(s); if (des[t] != INF) { cout << des[t] << endl; DFS(s, t); } else cout << "can't arrive"; cout << endl; } return 0; }
#include <iostream> #include <cstdio> using namespace std; #define MAXLEN 100 int Flag[MAXLEN] = {0}; int flag; int N; int Count; typedef struct { int len; int NodeA; int NodeB; } arrow; arrow Arrow[MAXLEN * MAXLEN]; int main() { void GetM(); void MiniSpanTree(); GetM(); MiniSpanTree(); return 0; } void GetM() { cin >> N; int temp; Count = 0; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { cin >> temp; if(temp != 0 && i >= j) { Arrow[Count].len = temp; Arrow[Count].NodeA = i; Arrow[Count].NodeB = j; Count++; } } } } void MiniSpanTree() { arrow Temp; int LenNum = 0; int Sum = 0; int i; for(i = 0; i < Count; i++) { for(int j = i; j < Count; j++) { if(Arrow[i].len > Arrow[j].len) { Temp.len = Arrow[i].len; Temp.NodeA = Arrow[i].NodeA; Temp.NodeB = Arrow[i].NodeB; Arrow[i].len = Arrow[j].len; Arrow[i].NodeA = Arrow[j].NodeA; Arrow[i].NodeB = Arrow[j].NodeB; Arrow[j].len = Temp.len; Arrow[j].NodeA = Temp.NodeA; Arrow[j].NodeB = Temp.NodeB; } } } flag = 0; int a , b; for(int i = 0; i < Count && LenNum < N - 1; i++) { a = Arrow[i].NodeA; b = Arrow[i].NodeB; if(Flag[a] == 0 && Flag[b] == 0) { Sum += Arrow[i].len; LenNum++; flag++; Flag[a] = flag; Flag[b] = flag; } else if(Flag[a] == 0 && Flag[b] != 0) { Sum += Arrow[i].len; LenNum++; Flag[a] = Flag[b]; } else if(Flag[a] != 0 && Flag[b] == 0) { Sum += Arrow[i].len; LenNum++; Flag[b] = Flag[a]; } else if(Flag[a] != 0 && Flag[b] != 0 && Flag[a] != Flag[b]) { Sum += Arrow[i].len; LenNum++; int t = Flag[b]; Flag[b] = Flag[a]; for(int j = 0; j < N; j++) { if(Flag[j] == t) Flag[j] = Flag[a]; } } } cout << Sum << endl; }
问题 I: 8.5.11 Disk Tree
题目描述
Hacker Bill has accidentally lost all the information from his workstation’s hard drive and he has no backup copies of its contents. He does not regret for the loss of the files themselves, but for the very nice and convenient directory structure that he had created and cherished during years of work. Fortunately, Bill has several copies of directory listings from his hard drive. Using those listings he was able to recover full paths (like “WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86”) for some directories. He put all of them in a file by writing each path he has found on a separate line. Your task is to write a program that will help Bill to restore his state of the art directory structure by providing nicely formatted directory tree.
输入
The first line of the input file contains single integer number N (1 <= N <= 500) that denotes a total number of distinct directory paths. Then N lines with directory paths follow. Each directory path occupies a single line and does not contain any spaces, including leading or trailing ones. No path exceeds 80 characters. Each path is listed once and consists of a number of directory names separated by a back slash (“").
Each directory name consists of 1 to 8 uppercase letters, numbers, or the special characters from the following list: exclamation mark, number sign, dollar sign, percent sign, ampersand, apostrophe, opening and closing parenthesis, hyphen sign, commercial at, circumflex accent, underscore, grave accent, opening and closing curly bracket, and tilde (“!#$%&’()-@^_`{}~”).
输出
Write to the output file the formatted directory tree. Each directory name shall be listed on its own line preceded by a number of spaces that indicate its depth in the directory hierarchy. The subdirectories shall be listed in lexicographic order immediately after their parent directories preceded by one more space than their parent directory. Top level directories shall have no spaces printed before their names and shall be listed in lexicographic order. See sample below for clarification of the output format.
样例输入
7 WINNT\SYSTEM32\CONFIG GAMES WINNT\DRIVERS HOME WIN\SOFT GAMES\DRIVERS WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86
样例输出
GAMES DRIVERS HOME WIN SOFT WINNT DRIVERS SYSTEM32 CERTSRV CERTCO~1 X86 CONFIG