
Acwing
Wa_Automata
这个作者很懒,什么都没留下…
展开
-
POJ - 1308 Is It A Tree?(并查集)
POJ - 1308 Is It A Tree?并查集用于判断是否存在环入度数组用于判断下述条件:(1)除了根节点外,每个节点都有且仅有一条指向它的边。(2)从根节点到每个其他节点都有一条唯一的有向边序列。#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int N = 10010;int a, b, cnt, idx;int s[原创 2022-03-26 18:28:59 · 482 阅读 · 0 评论 -
HDU - 1213 How Many Tables(并查集)
HDU - 1213 How Many Tables(并查集)#include <stdio.h> int f[1010],n,m; //这里是初始化,非常地重要,数组里面存的是自己数组下标的编号就好了。 void init() { for(int i=1;i<=n;i++) f[i]=i;} //这是找爹的递归函数,不停地去找爹,直到找到祖宗为止,其实就是去找犯罪团伙的最高领导人,//“擒贼先擒王”原则。int getf(int v)原创 2022-03-24 12:20:37 · 138 阅读 · 0 评论 -
HDU - 4370 0 or 1(Spfa,Dijkstra)
HDU - 4370 0 or 1(Spfa,Dijkstra)Spfa#include<iostream>#include<cstring>#include<queue>using namespace std;const int N = 310;int n,g[N][N],dist[N];bool st[N];void Spfa(int s){ queue<int> q; memset(dist,0x3f,sizeof原创 2022-03-19 16:05:15 · 244 阅读 · 0 评论 -
HDU - 4725 The Shortest Path in Nya Graph(虚拟点建图)
HDU - 4725 The Shortest Path in Nya Graph(虚拟源点)首先把每一层映成一个点,即第1层代表第n+1个节点如此类推,每一层与层内的建立一条权值为0的有向边,即add(i,x+n,0),然后点与点之间建立一条无向边这没什么好说了,最重要一步就是,第i层的所有点必须要与第i+1层和第i-1层建立一条有向边,即add(x+n-1,i,c),add(x+n+1,i,c)。Dijkstra#include<cstdio>#include<cstri原创 2022-03-16 19:47:09 · 586 阅读 · 0 评论 -
POJ - 1847 Tram(Floyd)
POJ - 1847 Tram(Floyd)#include<iostream>#include<cstring> using namespace std;const int N = 110;int g[N][N];int main(){ memset(g,0x3f,sizeof g); int n,A,B;cin>>n>>A>>B; for(int i=1;i<=n;i++) { int k;cin>>原创 2022-03-16 16:32:58 · 187 阅读 · 0 评论 -
POJ - 3660 Cow Contest(Floyd传递闭包)
POJ - 3660 Cow Contest(Floyd传递闭包)#include<iostream>using namespace std;const int N = 110;int w[N][N];int main(){ int n,m;cin>>n>>m; while(m--) { int a,b;cin>>a>>b; w[a][b]=1; } for(int k=1;k<=n;k++) for(in原创 2022-03-14 21:26:15 · 317 阅读 · 0 评论 -
POJ - 3259 Wormholes(Flody,SPFA)
POJ - 3259如果Farmer John从1号店出发,经过农场的时间,小于虫洞出发返回的时间,则他可以见到自己,紧接着可以判断图中是否存在负环。Flody算法和SPFA算法都可以实现SPFA下次补上Flody#include<iostream>#include<cstdio>using namespace std;const int N = 510, INF = 1e9; int dp[N][N],n,m,w;int floyd(){ for(i原创 2021-07-25 16:39:43 · 169 阅读 · 0 评论 -
POJ - 3268 Silver Cow Party(Dijkstra,Spfa)
POJ - 3268 Silver Cow Party(Dijkstra)Dijkstra邻接矩阵#include<iostream>#include<cstring>using namespace std;const int N = 1010;int n,m,x;int g[N][N],rg[N][N],dist[N],rdist[N];bool st[N];void dijkstra(int a[N][N],int dist[]){ memset(st,f原创 2022-03-13 20:30:09 · 568 阅读 · 0 评论 -
[kuangbin]专题四 最短路练习
POJ 2387 Til the Cows Come HomePOJ 2253 FroggerPOJ 1797 Heavy TransportationPOJ 3268 Silver Cow PartyPOJ 1860 Currency ExchangePOJ 3259 WormholesPOJ 1502 MPI MaelstromPOJ 3660 Cow ContestPOJ 2240 ArbitragePOJ 1511 Invitation CardsPOJ 31原创 2022-03-11 11:11:11 · 374 阅读 · 0 评论 -
HDU - 1495 非常可乐(BFS,数学)
HDU - 1495 非常可乐(BFS,数学)巨佬的数学解法#include<iostream>using namespace std;int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int main(){ int a,b,c; while(cin>>a>>b>>c&&(a&&b&&c)) { a/=gcd(b,c); if(a&原创 2022-03-11 10:50:01 · 421 阅读 · 0 评论 -
POJ - 2387 Til the Cows Come Home(Dijkstra,Spfa)
POJ - 2387 Til the Cows Come Home(最短路)题目大意:nnn个点mmm条边的无向图,求111到nnn的最短路径。Dijkstra邻接矩阵存图#include<iostream>#include<cstring>using namespace std;const int N = 1010;int n,m,g[N][N],dist[N]; // 存储1号点到每个点的最短距离bool st[N]; // 存储每个点的最短路是否已经确原创 2022-03-10 16:45:22 · 271 阅读 · 0 评论 -
POJ - 3414 Pots(BFS)
POJ - 3414 Pots(BFS)#include<iostream>#include<queue>using namespace std;typedef pair<int,int> PII;string s[105][105], ans;bool dist[105][105];int main(){ int a,b,c;cin>>a>>b>>c; queue<PII> q; q原创 2022-03-09 15:14:16 · 119 阅读 · 0 评论 -
POJ - 3087 Shuffle‘m Up
POJ - 3087 Shuffle’m Up#include<iostream>#include<set>using namespace std;string suff(string a, string b) //“洗牌”函数{ string ret; for (int i = 0; i < (int)a.size(); i++) ret += b[i], ret += a[i]; return ret;}int main(){ int T;cin原创 2022-03-09 15:02:23 · 128 阅读 · 0 评论 -
POJ - 1321 棋盘问题(DFS,递归与回溯)
POJ 1321 棋盘问题#include<cstdio>#include<cstring>char g[10][10];int n,k,ans,col[10];void dfs(int u,int cnt){ if(cnt==k)//如果此时满了,即使装满了,就ans++ { ans++; return; } if(u==n) return; dfs(u+1,cnt);//这一行不放棋子原创 2022-03-09 14:54:21 · 262 阅读 · 0 评论 -
UVA - 11624 Fire(BFS)
UVA - 116241.先求出所有格子被火覆盖的最短时间(多源bfs, 有多个火种)2.再从起点开始bfs, 只要我到达这个格子的时间 严格小于 这个格子被火覆盖的时间,就能走到这个格子3.走到边界的空地就返回#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;typedef pair<int,int> PI原创 2022-03-07 17:36:57 · 249 阅读 · 0 评论 -
POJ - 3279 Fliptile(状态压缩)
POJ - 3279 Fliptile#include<cstdio>#include<cstring>const int N = 20;const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};int n, m, ansf = 0;int g[N][N], b[N][N], ans[N][N];void turn(int x, int y) //翻转函数{ b[x][y] ^= 1; for (int i =原创 2022-03-01 20:28:18 · 178 阅读 · 0 评论 -
POJ - 2251 Dungeon Master(三维BFS)
POJ - 2251 Dungeon Master(三维BFS)http://poj.org/problem?id=2251题意: 给定一个三维空间,一个起点和终点,还有若干障碍物,人物从起点开始,只能往上下左右前后六个方向移动,问能否达到终点,若能,输出最短步数#include<iostream>#include<cstring>#include<queue>using namespace std;const int dx[]={-1,1,0,0,0,0}原创 2022-03-01 19:47:11 · 170 阅读 · 0 评论 -
POJ - 3126 Prime Path(线性筛+BFS)
POJ - 3126 Prime Path(BFS)#include<cstdio>#include<cstring>#include<queue>using namespace std;int dist[10010],primes[10010],st[10010],cnt;void get_primes() // 线性筛质数{ for (int i = 2; i <= 10000; i ++ ) { if (!s原创 2022-02-11 00:41:26 · 362 阅读 · 0 评论 -
HDU - 2612 Find a way(双向BFS)
https://vjudge.net/problem/HDU-2612http://acm.hdu.edu.cn/showproblem.php?pid=2612#include<iostream>#include<cstring>#include<queue>using namespace std;typedef pair<int,int> PII;const int N = 210;char g[N][N];int n,m,dist[N原创 2022-02-10 23:48:21 · 397 阅读 · 0 评论 -
POJ - 1180 Batch Scheduling(斜率优化DP)
POJ - 1180 Batch Scheduling(斜率优化DP)原创 2022-02-08 01:30:36 · 241 阅读 · 0 评论 -
POJ - 3017 Cut the Sequence(单调队列优化DP)
POJ - 3017 Cut the Sequence(单调队列优化DP)1.题意:给定一个长度为 NNN (N≤1e5)(N≤1e5)(N≤1e5)的序列 AAA ,要求把该序列分成若干段,在满足“““每段中所有数的和”””不超过MMM的前提下,让“““每段中所有数的最大值”””之和最小。2.分析朴素DP:状态:设f[i]表示将前i个数分为若干段,每段和不超过m时,最大值之和的最小值。转移方程:f[i]=min(f[j]+max(a[j+1],a[j+2],......a[i]))(∑k原创 2022-02-08 00:33:57 · 540 阅读 · 0 评论 -
POJ - 1821 Fence(单调队列优化DP)
POJ - 1821 Fence(单调队列优化DP)#include<cstdio>#include<algorithm>#include<deque>using namespace std;const int N=110,M=16010;struct Node{ int p,l,s;//单价;最高长度;必备点 bool operator <(const Node& t) const {return s<t.s原创 2022-02-04 23:00:21 · 342 阅读 · 0 评论 -
HDU - 5542 The Battle of Chibi(树状数组+DP)
UVA - 12983 The Battle of Chibi(树状数组+DP)HDU - 5542 The Battle of Chibi(树状数组+DP)#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int N = 1010, MOD = 1e9 + 7;int n,m,a[N],tr[N],f原创 2022-02-04 17:01:48 · 425 阅读 · 0 评论 -
POJ - 2185 Milking Grid(KMP)
POJ - 2185 Milking Grid(KMP)#include<stdio.h>#include<string.h>const int N = 10010, M = 80;char str[N][M];int ne[N],st[M];int main(){ int n,m;scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",str[i]); for(int j原创 2022-02-04 15:17:36 · 439 阅读 · 0 评论 -
POJ - 1442 Black Box(二叉堆)
POJ - 1442 Black Box(二叉堆)#include<cstdio>#include<queue>#include<vector>using namespace std;const int N = 30010;int a[N],b[N];int main(){ int n,m;scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i])原创 2022-02-04 14:33:05 · 243 阅读 · 0 评论 -
POJ - 2228 Naptime(环形结构DP)
POJ - 2228 Naptime(环形结构DP)#include <iostream>#include <cstring>using namespace std;const int N = 4000, INF = 0x3f3f3f3f;int n,m,w[N],f[2][N][2];void solve(){ for (int i = 2; i <= n; i ++ ) for (int j = 0; j <= m;原创 2022-02-04 12:45:07 · 489 阅读 · 0 评论 -
POJ - 3585 Accumulation Degree(二次扫描与换根法)
POJ - 3585 Accumulation Degree(二次扫描与换根法)#include<cstdio>#include<cstring>#include<vector>using namespace std;typedef pair<int,int> PII;const int N = 200010, INF = 0x3f3f3f3f;vector<PII> g[N];int d[N],f[N],deg[N原创 2022-02-03 20:32:43 · 409 阅读 · 0 评论 -
POJ - 1179 Polygon(区间DP)
POJ - 1179 Polygon(区间DP)#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int N = 110, INF = 1<<15;int w[N],f[N][N],g[N][N];char op[N];int main(){ int n;cin>>n; for(int i=1;i<原创 2022-02-03 18:25:00 · 253 阅读 · 0 评论 -
POJ - 1015 Jury Compromise
POJ - 1015 Jury Compromise#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int N = 210, M = 810, base = 400;int n, m;int p[N], d[N];int f[N][21][M];int ans[N];int main(){ int T = 1;原创 2022-02-02 00:46:20 · 286 阅读 · 0 评论 -
POJ - 2230 Watchcow(欧拉回路)
POJ - 2230 Watchcow(欧拉回路)#include<cstdio>#include<stack>using namespace std;const int N = 10010, M = 50010;int h[N],e[M*2],ne[M*2],idx=1;int n,m;stack<int> s,ans;void add(int a,int b){ e[idx]=b;ne[idx]=h[a];h[a]=idx++;}void e原创 2022-01-26 17:15:00 · 306 阅读 · 0 评论 -
POJ - 1201 Intervals(贪心+数据结构)
POJ - 1201 Intervals(贪心+数据结构)考虑把所有线段按照右端点 bbb 从小到大排序,依次考虑每一条线段的要求:如果已经满足要求则跳过否则尽量选择靠后的数(因为之后的线段的右端点都在这条线段的右边,这样容错更高)所以,我们可以建一个数组,f[i]f[i]f[i] 表示 iii 数字是否选择(填111或000),扫一遍[l,r][l,r][l,r] 区间求和,然后从后往前贪心放数即可。对于每条线段需要 O(r−l+1)O(r−l+1)O(r−l+1)。所以最坏情况下 O(n原创 2022-01-26 15:56:35 · 502 阅读 · 0 评论 -
POJ - 2259 Team Queue(队列模拟)
POJ - 2259 Team Queue(队列模拟)#include<iostream>#include<queue>using namespace std;const int N = 1010, M = 1000010;int teamid[M];int main(){ int n,C=1; while(cin>>n,n) { for(int i=0;i<n;i++) { int cnt;cin>&g原创 2022-01-24 17:05:50 · 566 阅读 · 0 评论 -
POJ - 1734 Sightseeing trip(Floyd传递闭包)
POJ - 1734 Sightseeing trip#include<iostream>#include<cstring>#include<vector>using namespace std;const int N = 110, INF = 0x3f3f3f3f;int d[N][N],g[N][N],pos[N][N];vector<int> path;void get_path(int i,int j){ if(p原创 2022-01-23 22:08:09 · 230 阅读 · 0 评论 -
POJ - 1094 Sorting It All Out(传递闭包)
POJ - 1094 Sorting It All Out#include<cstdio>#include<cstring>const int N = 26;int n,m;bool d[N][N],st[N];int check(){ for(int i=0;i<n;i++) if(d[i][i]) return 2; for(int i=0;i<n;i++) for(int j=0;j<i;j++) if(!d[i][j原创 2022-01-20 18:35:29 · 245 阅读 · 0 评论 -
POJ - 3662 Telephone Lines(双端队列+二分)
POJ - 3662 Telephone Lines题目描述到nnn有kkk条边可以免费升级,因此只需要求111~NNN的所有路径中第kkk + 111大的值的最小值,是最大最小值模型,因此可以使用二分求解对于区间[0,1000001][0,1000001][0,1000001]中的某一个点xxx:check(x)check(x)check(x)函数表示:::从111走到NNN,最少经过的长度大于xxx的边数的数量是否小于等于kkk,若是则返回truetruetrue,否则返回falsefa原创 2022-01-20 16:22:23 · 349 阅读 · 0 评论 -
POJ - 1742 Coins(DP+贪心)
HDU - 2844 CoinsPOJ - 1742 Coins算法(贪心+动态规划)时间复杂度O(N∗M)#include<cstdio>#include<cstring>using namespace std;const int N = 110, M = 100010;int n,m;int f[M],g[M];int v[N],s[N];int main(){ while(scanf("%d%d",&n,&m)&&原创 2021-07-30 10:01:15 · 157 阅读 · 0 评论 -
POJ - 2044 Weather Forecast(BFS)
POJ - 2044 Weather Forecast#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;const int N = 366;const int dx[]={-1,0,1,0,0},dy[]={0,1,0,-1,0};int n;bool st[N][3][3][7][7][7][7];int state原创 2022-01-20 00:00:06 · 362 阅读 · 0 评论 -
POJ - 1167 The Buses(DFS,迭代加深,剪枝)
POJ - 1167 The Buses(DFS,迭代加深,剪枝) O(CC60217)O(C_{C_{60}^2}^{17})O(CC60217)首先预处理出所有可能的线路。先枚举起点i,再枚举公差j,则i和j需要满足两个条件:由于i是起点,因此0 ~ i - 1中不能包含任何该序列的点,所以公差j至少是i + 1;由于0 ~ 59之间至少要包含两个点,因此i + j一定小于60;剩下的问题变成:最少从合法线路中选出多少条,才可以覆盖所有给定的公交车。由于总路线数量较多,最多原创 2022-01-19 01:16:37 · 181 阅读 · 0 评论 -
POJ - 3700 Missile Defence System(DFS,迭代加深,剪枝,贪心)
POJ - 3700 Missile Defence System(DFS,迭代加深,剪枝,贪心) O(n2n)O(n2^n)O(n2n)为了能遍历所有情况,我们首先考虑搜索顺序是什么。搜索顺序分为两个阶段:从前往后枚举每颗导弹属于某个上升子序列,还是下降子序列;如果属于上升子序列,则枚举属于哪个上升子序列(包括新开一个上升子序列);如果属于下降子序列,可以类似处理。分别记录当前每个上升子序列的末尾数up[],和下降子序列的末尾数down[]。这样在枚举时可以快速判断当前数是否可以接在某原创 2022-01-19 01:04:56 · 459 阅读 · 0 评论 -
POJ - 1084 Square Destroyer(IDA*算法)
POJ - 1084 Square Destroyer#include <cstdio>#include <cstring>#include <vector>using namespace std;const int N = 61; // 网格最大是 5 * 5 的,其中最多会有 5 * (5 + 1) * 2 = 60 个正方形,所以要开到 61int n, idx; // n 为网格规模,idx 为正方形数量int原创 2022-01-18 12:46:26 · 500 阅读 · 0 评论