图论 List
题目
#A 小 K 的农场 (Unaccepted)
#B 信息传递 (Unaccepted)
#C 最短路计数 (Accepted)
#D 通往奥格瑞玛的道路 (Accepted)
#E 公路修建 (Unaccepted)
#F 货车运输 (Unaccepted)
#G 图的m着色问题 (Unaccepted)
1. DFS & BFS
// 八皇后
// Au: GG #include <cstdio>
#include <cmath>
using namespace std;
int v[15], n, ans = 0;
bool f[15]; void dfs(int i) {
if (i > n) {
ans++;
if (ans <= 3) {
for (int j = 1; j <= n; j++) {
printf("%d ", v[j]);
}
printf("\n");
}
return;
}
for (int k = 1; k <= n; k++) {
if (f[k]) continue;
bool pd = true;
for (int j = 1; j < i; j++)
if (v[j] == k || abs(i - j) == abs(v[j] - k)) pd = false;
if (pd) {
v[i] = k;
f[k] = true;
dfs(i + 1);
f[k] = false;
v[i] = 0;
}
}
} int main() {
scanf("%d", &n);
dfs(1);
printf("%d", ans);
return 0;
}
// 马的遍历
// Au: GG #include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std; int n, m;
int dx[8] = { -2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int map[400 + 3][400 + 3];
struct bbc {
int x, y, n;
} s;
queue<bbc> q; int main() {
memset(map, -1, sizeof(map));
scanf("%d%d%d%d", &n, &m, &s.x, &s.y);
map[s.x][s.y] = 0;
q.push(s); while (!q.empty()) {
bbc a;
for (int i = 0; i < 8; i++) {
a = q.front();
int bx = a.x + dx[i];
int by = a.y + dy[i];
if (bx < 1 || bx > n || by < 1 || by > m || map[bx][by] != -1)
continue;
a.x = bx;
a.y = by;
a.n++;
q.push(a);
map[bx][by] = a.n;
}
q.pop();
} for (int k = 1; k <= n; k++) {
for (int r = 1; r <= m; r++)
printf("%-5d", map[k][r]);
printf("\n");
}
return 0;
}
/* Luogu P1126 机器人搬重物
* Au: GG
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 53;
int n, m, g[N][N], endx, endy;
bool d[N][N][4]; struct status {
int x, y, t;
char d;
} sta;
queue<status> q; char ltd(char dir) {
if (dir == 'N') return 'W';
else if (dir == 'W') return 'S';
else if (dir == 'S') return 'E';
else return dir = 'N';
} char rtd(char dir) {
if (dir == 'N') return 'E';
else if (dir == 'E') return 'S';
else if (dir == 'S') return 'W';
else return 'N';
} int did(char dir) {
if (dir == 'N') return 0;
else if (dir == 'E') return 1;
else if (dir == 'S') return 2;
else return 3;
} int main() {
int h;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &h);
if (h) {
g[i][j] = -1;
g[i + 1][j] = -1;
g[i][j + 1] = -1;
g[i + 1][j + 1] = -1;
}
} scanf("%d%d%d%d %c", &sta.x, &sta.y, &endx, &endy, &sta.d);
sta.x++; sta.y++; endx++; endy++;
sta.t = 0; d[sta.x][sta.y][did(sta.d)] = true; q.push(sta);
while (!q.empty()) {
status a = q.front(), b;
q.pop(); if (a.x == endx && a.y == endy) {
printf("%d\n", a.t); return 0;
}
d[a.x][a.y][did(a.d)] = true;
// printf("Status a(%d, %d, %c) = %d\n", a.x, a.y, a.d, a.t); // Creep & Walk & Run
for (int step = 1; step <= 3; step++) {
if (a.d == 'N') {
b.x = a.x - step; b.y = a.y;
} else if (a.d == 'S') {
b.x = a.x + step; b.y = a.y;
} else if (a.d == 'W') {
b.x = a.x; b.y = a.y - step;
} else {
b.x = a.x; b.y = a.y + step;
}
b.d = a.d; b.t = a.t + 1;
if (g[b.x][b.y] == 0 && !d[b.x][b.y][did(b.d)]) {
if (b.x > 1 && b.x < n + 1 && b.y > 1 && b.y < m + 1)
q.push(b);
else break;
} else break;
} // Left
b.x = a.x; b.y = a.y; b.t = a.t + 1;
b.d = ltd(a.d);
if (!d[b.x][b.y][did(b.d)])
if (b.x > 1 && b.x < n + 1 && b.y > 1 && b.y < m + 1)
q.push(b); // Right
b.x = a.x; b.y = a.y; b.t = a.t + 1;
b.d = rtd(a.d);
if (!d[b.x][b.y][did(b.d)])
if (b.x > 1 && b.x < n + 1 && b.y > 1 && b.y < m + 1)
q.push(b);
} printf("-1\n");
return 0;
}
/*
9 10
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1 0
7 2 2 7 S
*/ /*
问题(估计是致命问题)给你一个图(点阵,不是块阵)
0 0 0 0 0 0 0
0 S 1 T 1 0 0
机器人在S处面向右方,根据你82-97行的前进代码,机器人可以到达T处,但是事实上并不能到达,所以题目数据的图(点阵)如下
0 0 0 0 0 0-1-1 0 0 0
0 0 0 0 0 0-1-1-1-1 0
0 0 V-1-1 V 0 T-1-1 0
0 0-1-1-1 0 0 0 0 0 0
0 0-1-1 0 0-1-1 0 0 0
0 0 V 0 0-1-1-1 0 0 0
0 0 0-1-1-1-1 0 0 0 0
0 0 S-1-1-1 0 0 0 0 0
-1-1 0 0 0 0 0 0-1-1 0
-1-1 0 0 0 0 0 0-1-1 0
7步的行径路径给你了 左转(面向右边)
左转(面向上方)
walk
run(这步其实不能走,但是按照你的代码是可以走的)
右转(面向右边)
run(这步其实也不能走,但是按照你的代码也是可以走的)
walk 以上七步S是起点V是途经点T是终点 连这个错误你都犯,你可以去面壁一天了。。。(什么时候可以穿墙了。。。)
还有,让zzh去面壁半天,连这么简单的错误都看不出来。
我先走了
——8:45 by dph
*/
/* P1118 [USACO06FEB]数字三角形Backward Digit Su…
* Au: GG
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 15;
int n, sum, tri[N][N], d[N];
bool v[N]; void triangle() { // triangle table
for (int i = 1; i <= n; i++) tri[i][1] = tri[i][i] = 1;
for (int i = 2; i <= n; i++)
for (int j = 2; j < i; j++)
tri[i][j] = tri[i - 1][j] + tri[i - 1][j - 1]; // for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= i; j++)
// printf("%d ", tri[i][j]);
// printf("\n");
// }
} bool dfs(int f, int s) {
// printf("dfs(%d, %d, d = %d)\n", f, s, d[f - 1]);
if (f > n) {
if (s == sum) return true;
else return false;
}
for (int i = 1; i <= n; i++)
if (!v[i] && s + i * tri[n][f] <= sum) {
v[i] = true; d[f] = i;
if (dfs(f + 1, s + i * tri[n][f])) return true;
v[i] = false;
}
return false;
} int main() {
scanf("%d%d", &n, &sum);
triangle(); if (dfs(1, 0)) {
for (int i = 1; i <= n; i++) printf("%d ", d[i]);
} return 0;
}
/* Luogu P1141 01迷宫
* Au: GG
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1000 + 5;
int n, m, g[N][N]; int vis[N][N], vs, vcnt[100000 + 5];
int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1}; struct point {
int x, y;
}; int read() {
char a = getchar();
while (a != '0' && a != '1') a = getchar();
return a - '0';
} bool check(point i) {
if (i.x < 1 || i.x > n) return false;
if (i.y < 1 || i.y > n) return false;
return true;
} int bfs(int u, int v) {
if (vis[u][v]) return vcnt[vis[u][v]]; int cnt = 0; vs++;
queue<point> q;
point a, b; a.x = u, a.y = v;
q.push(a); while (!q.empty()) {
a = q.front();
q.pop(); if (vis[a.x][a.y]) continue;
vis[a.x][a.y] = vs; cnt++; for (int i = 0; i < 4; i++) {
b.x = a.x + dx[i];
b.y = a.y + dy[i];
if (check(b) && (g[b.x][b.y] != g[a.x][a.y]) && !vis[b.x][b.y]) q.push(b);
}
} return vcnt[vs] = cnt;
} int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
g[i][j] = read();
}
} int xx, yy;
while (m--) {
scanf("%d%d", &xx, &yy);
printf("%d\n", bfs(xx, yy));
} return 0;
}
2. 最小生成树
// 最小生成树
// Au: GG #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std; int n, m, f[5000 + 3], sum;
struct edge {
int x, y, w;
bool operator < (const edge &a) const {
return w > a.w;
}
} temp;
priority_queue<edge> G; int find(int x) {
return (f[x] == x) ? x : f[x] = find(f[x]);
}
void join(int x, int y) {
f[find(y)] = find(x);
} int main() {
int x, y, z, flag = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
temp.x = x; temp.y = y; temp.w = z; G.push(temp);
}
for (int i = 1; i <= n; i++) f[i] = i;
while (!G.empty()) {
temp = G.top(); G.pop();
if (find(temp.x) != find(temp.y)) join(temp.x, temp.y), sum += temp.w, flag++;
}
if (flag < n - 1) printf("orz\n");
else printf("%d\n", sum);
return 0;
}
3. 并查集
/* 并查集
* Au: GG
*/
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 10000 + 3;
int n, m, z, x, y, f[maxn];
int find(int x) {
return (f[x] == x) ? x : f[x] = find(f[x]);
}
inline void join(int x, int y) {
f[find(y)] = find(x);
}
int main() {
scanf("%d%d", &n, &m);
while (n--) f[n] = n;
while (m--) {
scanf("%d%d%d", &z, &x, &y);
if (z == 1) {
join(x, y);
} else {
if (find(x) == find(y)) printf("Y\n");
else printf("N\n");
}
}
return 0;
}
图论 List的更多相关文章
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- 并查集(图论) LA 3644 X-Plosives
题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...
- NOIp 2014 #2 联合权值 Label:图论 !!!未AC
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- [转] POJ图论入门
最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...
- HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Codeforces 553C Love Triangles(图论)
Solution: 比较好的图论的题. 要做这一题,首先要分析love关系和hate关系中,love关系具有传递性.更关键的一点,hate关系是不能成奇环的. 看到没有奇环很自然想到二分图的特性. 那 ...
- 图论(floyd算法):NOI2007 社交网络
[NOI2007] 社交网络 ★★ 输入文件:network1.in 输出文件:network1.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在社交网络( ...
- 【图论】【宽搜】【染色】NCPC 2014 A Ades
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1787 题目大意: N个点M条无向边(N,M<=200000),一个节点只能有一个 ...
随机推荐
- PCB布线设计-模拟和数字布线的异同(转)
工程领域中的数字设计人员和数字电路板设计专家在不断增加,这反映了行业的发展趋势.尽管对数字设计的重视带来了电子产品的重大发展,但仍然存在,而且还会一直存在一部分与模拟或现实环境接口的电路设计.模拟和数 ...
- NOPI导入导出EXCEL
一.简介 1. 什么是NPOI NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office 97 ...
- 实验报告一&第三周学习总结
一.实验报告 1.打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数" ...
- Super Mario HDU 4417 主席树区间查询
Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...
- C/C++二维数组名和二级指针
转载 :https://blog.csdn.net/wu_nan_nan/article/details/51741030 作者:吴一奇 1. 指针1.1 一个指针包含两方面:a) 地址值:b) 所 ...
- [常用类]StringBuffer 类,以及 StringBuilder 类
线程安全,可变的字符序列. 字符串缓冲区就像一个String ,但可以修改. 在任何时间点,它包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容. 字符串缓冲区可以安全地被多个线程使 ...
- CSS-子盒子撑开父盒子,让父盒子的高随内容自适应
方法一: height:auto!important; height:200px; min-height:200px; ie6并不支持min-height.ie7,opera,火狐没有问题. 方法二: ...
- 数组去重ES6
原文链接:https://juejin.im/post/5b17a2c251882513e9059231 1,去除简单类型 //ES6中新增了Set数据结构,类似于数组,但是 它的成员都是唯一的 ...
- Vue.js——60分钟组件快速入门(上篇)二
来源:https://www.cnblogs.com/keepfool/p/5625583.html 组件简介 组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小 ...
- Vue.js——60分钟快速入门 一
来源:https://www.cnblogs.com/keepfool/p/5619070.html Vue.js介绍 Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组 ...