图论入门训练内容
D - 鸣人和佐助
题目:已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
我的做法:这个题不是简单的最短路问题,多了一个状态(查克拉数量),所以可以将一个点拆成T种状态,用这T种状态的关系相互更新即可,我用的是迪杰斯特拉,排序关键字是时间。
代码:
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<unordered_map>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> PII;
const int N = 1e5 + 5, M = 1e6 + 5;
const int INF = 1e9;
char a[205][205];
int n, m, t;
int sx, sy;
int biaoji[205][205][10];//标记数组
int d[205][205][10];//距离(时间)
struct A {
int x, y;
int st;//步数
int hua;//花费
bool operator<(const A& b)const {
return st > b.st;
}
};
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
bool check(int x, int y, int now) {
if (x<1 || x>n || y<1 || y>m)return 0;
return 1;
}
int bfs()
{
priority_queue<A>q;
memset(d, 0x3f, sizeof d);
d[sx][sy][0] = 0;
q.push({ sx,sy,0,0 });
while (q.size()) {
A now = q.top();
q.pop();
int x = now.x, y = now.y, st = now.st, hua = now.hua;
if (a[x][y] == '+')return st;
if (biaoji[x][y][hua])continue;
biaoji[x][y][hua] = 1;
for (int i = 0; i < 4; i++) {
int bx = x + dx[i];
int by = y + dy[i];
if (check(bx, by,hua)) {
if (a[bx][by] == '#') {//需要花钱
if (hua+1<=t&&d[bx][by][hua + 1] > d[x][y][hua] + 1) {
d[bx][by][hua + 1] = d[x][y][hua] + 1;
q.push({ bx,by,d[bx][by][hua + 1],hua + 1 });
}
}
else {//不需要花钱
if ( d[bx][by][hua] > d[x][y][hua] + 1) {
d[bx][by][hua] = d[x][y][hua] + 1;
q.push({ bx,by,d[bx][by][hua],hua });
}
}
}
}
}
return -1;
}
int main()
{
cin >> n >> m >> t;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '@') {
sx = i, sy = j;
}
}
}
cout << bfs();
return 0;
}
B - 网络寻路
题意:X 国的一个网络使用若干条线路连接若干个节点。节点间的通信是双向的。某重要数据包,为了安全起见,必须恰好被转发两次到达目的地。该包可能在任意一个节点产生,我们需要知道该网络中一共有多少种不同的转发路径。
源地址和目标地址可以相同,但中间节点必须不同。
如图 11 所示的网络。
1→2→3→11→2→3→1 是允许的。
1→2→1→21→2→1→2 或者 1→2→3→21→2→3→2 都是非法的。
我的做法:
枚举中间的两个中转点i,j,答案就是(能够直接到达i的点数×能够直接到达j的点数),这个直接到达i的点数不能包括j这个点。
代码::
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> PII;
typedef double db;
const int N = 1e5 + 5, M = 1e6 + 5;
const int INF = 1e9;
int n, m, t;
int du[N];
int h[N], e[M], ne[M];
int idx;
void add(int a, int b) {
++idx;
e[idx] = b, ne[idx] = h[a],h[a] = idx;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
add(a, b);
add(b, a);
du[a]++, du[b]++;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = h[i]; j; j = ne[j]) {
ans += (du[i] - 1) * (du[e[j]] - 1);
}
}
cout << ans;
}
C - Channel Allocation
题意:给一个图染色,要求相邻点的颜色不同,问最少染色数目
做法:爆搜
代码:
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
typedef unsigned long long ll;
typedef pair<int, int> PII;
typedef double db;
const int N = 1e5 + 5, M = 1e6 + 5;
const int INF = 1e9;
int n, m, t;
int du[N];
int h[N], e[M], ne[M];
int idx;
map<char, int>mp;//
int cnt = 0;
int g[28][28];
int ans = 0;
int biaoji[28];
bool check(int now, int co) {
for (int i = 1; i <= n; i++) {
if (i == now)continue;
if (g[now][i]&&biaoji[i] == co)return 0;
}
return 1;
}
void dfs(int def, int sum) {//当前点,最少需要的颜色数
//当前点没有被染色,
if (ans)return;
if (def == n + 1) {
ans =sum;
return;
}
for (int i = 1; i <= sum; i++) {
if (check(def, i)) {
biaoji[def] = i;//染成i颜色
dfs(def + 1, sum);
return;
}
}
biaoji[def] = sum + 1;//染成sum+1
dfs(def + 1, sum + 1);
}
int main()
{
for (char i = 'A'; i <= 'Z'; i++) {//映射
mp[i] = ++cnt;
}
//相邻的点不能是同种颜色,问最少需要多少颜色?
while (cin >> n, n) {
ans = 0;
memset(biaoji, 0, sizeof biaoji);
memset(g, 0, sizeof g);//初始化
for (int i = 1; i <= n; i++) {//建图
string s;
cin >> s;
int a = mp[s[0]];
for (int i = 2; i < s.size(); i++) {
int b = mp[s[i]];
g[a][b] = 1;
}
}
biaoji[1] = 1;
dfs(1, 0);
cout << ans<<" ";
cout << "channel";
if (ans > 1)cout << 's';
cout << ' ';
cout << "needed." << endl;
}
}