UVALive 7414 Sibling Rivalry
2016-08-28 14:18
218 查看
题意:n个点m条边的有向图,有一个起点和一个终点,每次兄贵选择从abc三个数中选择一个数作为你前进的步数,问兄贵最多选择多少次你一定可以到达终点,如果无论如何都到大不了终点则输出IMPOSSIBLE.
分析:假设最多需要k步我们一定可以到达终点,那么对于k-1步来说,他下一步走a,b,c步一定都可以到达终点,同理,k-2步的点下一步走a,b,c步一定可以到达k-1之前能到达的点,所以我们可以一遍bfs,如果结束了还没有找到起点,那么一定是无解。
分析:假设最多需要k步我们一定可以到达终点,那么对于k-1步来说,他下一步走a,b,c步一定都可以到达终点,同理,k-2步的点下一步走a,b,c步一定可以到达k-1之前能到达的点,所以我们可以一遍bfs,如果结束了还没有找到起点,那么一定是无解。
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #include<cstdio> #include<set> #include<map> #include<vector> #include<cstring> #include<stack> #include<cmath> #include<queue> #include <unordered_map> #define INF 0x3f3f3f3f #define eps 1e-9 #define MOD 1000000007 using namespace std; typedef long long ll; int n,m,a,b,c,jud[51],color[51],val[51]; struct Matrix { ll val[51][51]; int x,y; friend Matrix operator *(const Matrix a,const Matrix b) { Matrix c = {0}; c.x = a.x,c.y = b.y; for(int i = 1;i <= a.x;i++) for(int j = 1;j <= b.y;j++) for(int k = 1;k <= b.x;k++) c.val[i][j] = (c.val[i][j] + a.val[i][k]*b.val[k][j]) % MOD; return c; } }G,Ga,Gb,Gc; Matrix ksm(Matrix a,ll b) { Matrix c = a; b--; while(b) { if(b & 1) c = c*a; a = a*a; b>>=1; } return c; } int main() { while(~scanf("%d%d%d%d%d",&n,&m,&a,&b,&c)) { G.x = G.y = n; memset(G.val,0,sizeof(G.val)); memset(jud,0,sizeof(jud)); memset(color,0,sizeof(color)); for(int i = 1;i <= m;i++) { int x,y; scanf("%d%d",&x,&y); G.val[y][x] = 1; } Ga = ksm(G,a),Gb = ksm(G,b),Gc = ksm(G,c); deque <int> s; s.push_back(n); jud = true; val = 0; while(!s.empty() && !jud[1]) { int u = s.front(); s.pop_front(); for(int v = 1;v <= n;v++) { if(jud[v]) continue; if(Ga.val[u][v]) color[v] |= 1; if(Gb.val[u][v]) color[v] |= 2; if(Gc.val[u][v]) color[v] |= 4; if(color[v] == 7) { jud[v] = true; val[v] = val[u] + 1; if(v == 1) break; s.push_back(v); } } } if(!jud[1]) cout<<"IMPOSSIBLE"<<endl; else cout<<val[1]<<endl; } }
相关文章推荐
- UVALive 3305 Tour(DP)
- Cellphone Typing - UVaLive 6133 Trie树
- UVALive 6511 Term Project
- Exploring Pyramids UVALive - 3516 题解
- uvalive 3401 poj 2741 Colored Cubes
- UVALive 6434 Number Assignment(dp)
- Repeated Substrings(UVAlive 6869)
- UVALive 4270 Discrete Square Roots 模方程,数论
- 【拓扑排序】 UVALive 6393 Self-Assembly
- UVALive 6609(Minimal Subarray Length)维护递增序列|RMQ
- UVALive 7070 The E-pang Palace(2014广州赛区现场赛)
- UVALIVE 2675 Peals (POJ 1260 HDU 1300)
- UVALive Problem 7079 How Many Maos Does the Guanxi Worth(暴力枚举+最短路)——2014ACM/ICPC亚洲区广州站
- UVa Live 7040 (二项式反演+线性求逆元)
- UVALive 6919 A game for kids
- UVALive 6424 Russian Dolls 贪心
- UVALive 3662 Another Minimum Spanning Tree 曼哈顿最小生成树
- UVALive - 7045 Last Defence (更相减损术)
- UvaLive 4255 Guess(拓扑排序)
- UVaLive 6847 Zeroes (找规律,水题)