入门经典-训练指南5.1 图论基础例题
2014-02-15 00:32
405 查看
UVA - 11624
Fire!
UVA - 10047
The Monocycle
UVA - 10054
The Necklace
UVALive - 4255
Guess
Fire!
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 1010; const int INF = 1000000000; typedef pair<int,int>PII; char mp[maxn][maxn]; int t_fire[maxn][maxn] ,vis[maxn][maxn] , R , C; const int dx[4] = {0 , -1 , 0 , 1}; const int dy[4] = {1 , 0 , -1 , 0}; bool check_f(int x,int y){ if(x<0 || x>=R || y<0 || y>=C) return false; if(vis[x][y] || mp[x][y]=='#') return false; return true; } void bfs_fire() { memset(vis , 0 , sizeof(vis)); memset(t_fire , 0x7f , sizeof(t_fire)); queue<PII>Q; for(int i = 0;i<R ;i++){ for(int j=0;j<C;j++){ if(mp[i][j] == 'F'){ Q.push(make_pair(i*C+j , 0)); vis[i][j] = 1; } } } while(!Q.empty()){ PII cur = Q.front() ; Q.pop(); int x = cur.first / C ,y = cur.first % C ,t = cur.second; t_fire[x][y] = t; for(int d = 0; d<4 ;d++){ int nx = x + dx[d] , ny = y + dy[d]; if(check_f(nx,ny)) { Q.push(make_pair(nx*C + ny , t+1)); vis[nx][ny] = 1; } } } } bool check_j(int x,int y,int t){ if(x<0 || x>=R || y<0 || y>=C) return false; if(vis[x][y] || mp[x][y] == '#') return false; return t < t_fire[x][y]; } int bfs_joe() { memset(vis , 0 , sizeof(vis)); queue<PII>Q; for(int i = 0;i<R ;i++){ for(int j=0;j<C;j++){ if(mp[i][j] == 'J'){ Q.push(make_pair(i*C+j , 0)); vis[i][j] = 1; goto step1; } } } step1 :; while(!Q.empty()){ PII cur = Q.front() ; Q.pop(); int x = cur.first / C ,y = cur.first % C ,t = cur.second; if(x==0 || x==R-1 || y == 0 || y==C-1) return t+1; for(int d = 0; d<4 ;d++){ int nx = x + dx[d] , ny = y + dy[d]; if(check_j(nx,ny,t+1)) { Q.push(make_pair(nx*C + ny , t+1)); vis[nx][ny] = 1; } } } return -1; } int main() { int T; scanf("%d" , &T); while(T--){ scanf("%d%d" , &R , &C); for(int i=0;i<R;i++){ scanf("%s" , mp[i]); } bfs_fire(); int ans = bfs_joe(); if(ans < 0) printf("IMPOSSIBLE\n"); else printf("%d\n" ,ans); } return 0; }
UVA - 10047
The Monocycle
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int dx[4] = {0 , -1 , 0 , 1}; const int dy[4] = {1 , 0 , -1 , 0}; struct STATE{ int x,y,color,dir ,time; STATE(int x1,int y1,int c1,int d1,int t1) : x(x1) ,y(y1) ,color(c1) , dir(d1) ,time(t1) {}; }; char mp[30][30]; int R , C, vis[30][30][5][4]; void get_next(const STATE &a , STATE &b ,int kind){ b = a , b.time ++; if(kind == 0){ b.x += dx[a.dir] , b.y += dy[a.dir] , b.color ++ , b.color %= 5 ; } else if(kind == 1){ b.dir = (b.dir + 3) % 4; } else b.dir = (b.dir + 1) % 4; } bool check(const STATE & s){ if(s.x<0 || s.x >= R || s.y < 0 || s.y >= C || mp[s.x][s.y] == '#') return false; if(vis[s.x][s.y][s.color][s.dir]) return false; return true; } int BFS() { memset(vis , 0 ,sizeof(vis)); queue<STATE>Q; for(int i=0;i<R;i++) for(int j=0;j<C;j++)if(mp[i][j] == 'S'){ Q.push(STATE(i,j,0,1,0)) , vis[i][j][0][1] = 1; } while(!Q.empty()){ STATE cur = Q.front() ; Q.pop(); if(mp[cur.x][cur.y] == 'T' && cur.color == 0) return cur.time; STATE next = cur; for(int kind = 0 ; kind < 3 ; kind ++){ get_next(cur , next , kind); if(check(next)) { Q.push(next) ; vis[next.x][next.y][next.color][next.dir] = 1; } } } return -1; } int main() { int cas = 1; while(scanf("%d%d" ,&R , &C)!=EOF && R + C){ for(int i=0;i<R;i++) scanf("%s" , mp[i]); int ans = BFS(); if(cas != 1) printf("\n"); printf("Case #%d\n" , cas++); if(ans < 0) printf("destination not reachable\n"); else printf("minimum time = %d sec\n" ,ans); } return 0; }
UVA - 10054
The Necklace
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxc = 55; int cnt[maxc] , adj[maxc][maxc] ; int vis[maxc]; void dfs(int u){ vis[u] = 1; for(int i=0;i<=50;i++) { if(adj[u][i] && !vis[i]) dfs(i); } } bool conected(int s){ memset(vis , 0, sizeof(vis)); dfs(s); for(int i=1;i<=50;i++) if(cnt[i]){ if(!vis[i]) return false; } return true; } void solve(int u){ for(int i=1;i<=50;i++) if(adj[u][i]) { adj[u][i]-- , adj[i][u]--; solve(i); printf("%d %d\n" , i ,u); } } inline int nextint(){int x; scanf("%d" ,&x); return x;} int main() { //freopen("input.txt" , "r" ,stdin); int T , cas = 1; T = nextint(); while(T--){ memset(cnt , 0 ,sizeof(cnt)); memset(adj , 0 , sizeof(adj)); int N; N = nextint(); for(int i = 0; i<N ;i++){ int x ,y; x=nextint(), y=nextint(); cnt[x]++ , cnt[y]++; adj[x][y]++, adj[y][x]++; } bool ok = true; int s=1; while(cnt[s]==0) s++; for(int i=1 ; i<50 ;i++){ if(cnt[i]&1) {ok = false ; break; } } if(!conected(s)) ok = false; printf("Case #%d\n" ,cas++); if(ok){ solve(s); } else printf("some beads may be lost\n"); if(T) printf("\n"); } return 0; }
UVALive - 4255
Guess
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 12; int small[maxn][maxn], eq[maxn][maxn] , in[maxn]; int sum[maxn] , n; inline int nextint(){int x; scanf("%d" , &x); return x; } inline char nextchar() {char c=getchar();while(c==' ' || c=='\n') c = getchar(); return c;} void topu(){ queue<int>Q; int inq[maxn]={0}; for(int i=0;i<=n;i++) { if(!in[i]) Q.push(i), sum[i]=0 , inq[i] = 1; } while(!Q.empty()){ int u = Q.front(); Q.pop(); inq[u] = 0; for(int i=0;i<=n;i++) if(eq[u][i] && sum[u]>sum[i]){ sum[i] = sum[u]; if(!inq[i]) Q.push(i) , inq[i] = 1; } for(int i=0;i<=n;i++) if(small[u][i]) { sum[i] = sum[u]+1; if(!inq[i]) Q.push(i) , inq[i] = 1; } } } int main() { int T=nextint(); while(T--){ memset(small, 0 ,sizeof(small)); memset(eq , 0 , sizeof(eq)); memset(in , 0, sizeof(in)); memset(sum , 0, sizeof(sum)); n=nextint(); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++){ char c = nextchar(); if(c=='+') small[i-1][j] = 1 , in[j]++; else if(c=='-') small[j][i-1] = 1 ,in[i-1]++; else eq[i-1][j] = eq[j][i-1] = 1; } topu(); for(int i=1;i<=n;i++) printf("%d " , sum[i] - sum[i-1]); printf("\n"); } return 0; }
相关文章推荐
- 算法竞赛入门经典训练指南-P4例题3 分金币问题总结
- 入门经典训练指南第三章例题1.UVa 11995 I Can Guess the Data Structure!
- uva297 - Quadtrees 入门经典II 第六章 数据结构基础 例题6-11
- 算法竞赛入门经典——训练指南 例题2
- uva10562 - Undraw the Trees 入门经典II 第六章数据结构基础 例题6-17
- UVa 11426--算法竞赛入门经典训练指南
- 入门经典训练指南后缀数组算法小弯
- UVALive 3211 (训练指南)图论例题9 2-sat+二分
- 凸包代码,摘自入门经典训练指南
- uva11988 - Broken Keyboard (a.k.a. Beiju Text) 入门经典II 第六章数据结构基础 例题6-4
- LA4119--算法入门经典训练指南
- 【索引】算法竞赛入门经典-第11章 图论模型与算法
- 算法竞赛入门经典:第五章 基础题目选解 5.11果园里的树
- 算法竞赛入门经典:第六章 数据结构基础 6.12迷宫路径
- Spark快速入门指南 – Spark安装与基础使用
- JavaScript学习指南之第一章Hello JavaScript!最基础的JavaScript入门
- ELKStack的基础入门和中文指南
- [算法入门经典] 例题7-3 倒水问题 | HDU 1495
- 入门经典_Chap06_例题[二]:二叉树的指针及数组实现
- Elasticsearch: 权威指南---基础入门