【bessie come home】 /* ID: wangqia6 TASK: comehome LANG: C++ */ #include <fstream> #include <cstring> using namespace std; const long INF = 0x01010101; const long LEN = 200000; const int NMAX = 55; long dist[NMAX],g[NMAX][NMAX],que[LEN],head = 0,tail = -1; bool vis[NMAX]; void prework() { memset(g,0x01,sizeof(g)); memset(vis,0,sizeof(vis)); memset(dist,0x01,sizeof(dist)); return; } int ord(char ch) { if ((ch >= 'A') && (ch <= 'Z')) return int (ch - 'A'); else if ((ch >= 'a') && (ch <= 'z')) return int (ch - 'a') + 26; } void initdata() { ifstream cin ("comehome.in"); int n,w,ka,kb; char a,b; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; ka = ord(a); kb = ord(b); cin >> w; if (g[ka][kb] > w) { g[ka][kb] = w; g[kb][ka] = w; } } cin.close(); return; } void push(int s) { vis[s] = true; tail++; que[tail] = s; return; } void spfa(int s) { push(s); dist[s] = 0; int v,u; while (head <= tail) { v = que[head]; vis[v] = false; head++; for (u = 0; u <= NMAX; u++) if (dist[v] + g[v][u] < dist[u]) { dist[u] = dist[v] + g[v][u]; if (! vis[u]) push(u); } } return; } void outitdata() { ofstream cout ("comehome.out"); long ans = INF; int x; for (int i = 0; i < 25; i++) if (dist[i] < ans) { ans = dist[i]; x = i; } cout << char ('A' + x) << ' ' << ans << endl; cout.close(); return; } int main() { prework(); initdata(); spfa(25); outitdata(); return 0; } 【fractions to decimals】 /* ID: wangqia6 TASK: fracdec LANG: C++ */ #include <fstream> #include <cmath> #include <cstring> using namespace std; const long LIMIT = 76; const double ZERO = 1e-5; const long MAXNUM = 110000; ifstream cin ("fracdec.in"); ofstream cout ("fracdec.out"); bool vis[MAXNUM]; long tot; void add_tot() { tot++; if (tot == LIMIT) { cout << endl; tot = 0; } return; } long gcd(long a,long b) { if (b) return gcd(b,a % b); else return a; } int main() { long a,b,x,i; cin >> a >> b; if (! (a % b)) { cout << a / b << ".0" << endl; cin.close(); cout.close(); return 0; } x = gcd(a,b); a /= x; b /= x; x = a / b; cout << x << '.'; if (x) tot = 2 + long(floor(log10(x + ZERO))); else tot = 2; a %= b; long unrepeat = 0; x = b; while (! (x % 10)) { unrepeat++; x /= 10; } while (! (x & 1)) { x = x >> 1; unrepeat++; } while (! (x % 5)) { x = x / 5; unrepeat++; } for (i = 1; i <= unrepeat; i++) { a *= 10; cout << a / b; add_tot(); a %= b; } if (! a) { if (tot != 0) cout << endl; cin.close(); cout.close(); return 0; } cout <<'('; add_tot(); memset(vis,0,sizeof(vis)); while (! vis[a]) { vis[a] = true; a *= 10; cout << a / b; add_tot(); a %= b; } cout << ')' << endl; cin.close(); cout.close(); return 0; }