今天又来打卡了,写了4道题:P5318,P1113,P4017,P1807,感觉还是学到了不少东西的:
首先是P5318这个就是深搜和广搜,但是又多了一点心意,得提前排序然后再输入:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
struct edge {
int st, en;
edge() {};
edge(int _st, int _en)
{
st = _st;
en = _en;
}
};
edge e[1000005];
int n, m;
vector <int> graph[100005];
int u, v;
queue <int> q;
int vis[100005];
bool cmp(edge a, edge b)
{
if (a.st == b.st)
return a.en < b.en;
else
return a.st < b.st;
}
void Init()
{
for (int i = 1; i <= n; i++)
{
vis[i] = 0;
}
}
void dfs(int start, int cnt)
{
if (cnt == n)
return;
for (int i = 0; i < graph[start].size(); i++)
{
int tmp = graph[start][i];
if (!vis[tmp])
{
printf("%d ", tmp);
vis[tmp] = 1;
dfs(tmp, cnt + 1);
}
}
}
void bfs()
{
Init();
q.push(1);
printf("%d ", 1);
vis[1] = 1;
while (!q.empty())
{
int tmp = q.front();
q.pop();
for (int i = 0; i < graph[tmp].size(); i++)
{
if (!vis[graph[tmp][i]])
{
printf("%d ", graph[tmp][i]);
q.push(graph[tmp][i]);
vis[graph[tmp][i]] = 1;
}
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> u >> v;
e[i] = edge(u, v);
}
sort(e, e + m, cmp);
for (int i = 0; i < m; i++)
{
graph[e[i].st].push_back(e[i].en);
}
Init();
vis[1] = 1;
printf("%d ", 1);
dfs(1, 1);
cout << endl;
bfs();
return 0;
}
P1113这个是AOE路径的问题,我是用动态规划写的,我不太会动态规划,还是写了比较久的:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
struct edge {
int u, w;
edge() {};
edge(int _u, int _w)
{
u = _u;
w = _w;
}
};
int n, id, len;
vector <edge> graph[10004];
int f[100004];
int ans = 0;
void pos()
{
f[0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < graph[i].size(); j++)
{
f[i] = max(f[i], f[graph[i][j].u] + graph[i][j].w);
}
ans = max(ans, f[i]);
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> id >> len;
int x;
while(cin >> x && x != 0)
{
graph[id].push_back(edge(x, len));
}
graph[id].push_back(edge(x, len));
}
pos();
cout << ans;
return 0;
}
P4017这个是拓扑排序的题,不过有个题解我觉的和有意思大家打开题解的这个
我觉的很有思想,大家可以重点看看:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int N = 80112002;
int n, m;
int into[5005];
int vis[5005];
int ans[5005];
int outto[5005];
vector <int> graph[5005];
queue <int> q;
int an = 0;
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
graph[x].push_back(y);
outto[x]++;
into[y]++;
}
for (int i = 1; i <= n; i++)
{
if (into[i] == 0)
{
q.push(i);
ans[i] = 1;
}
}
while (!q.empty())
{
int tmp = q.front();
q.pop();
for (int i = 0; i < graph[tmp].size(); i++)
{
into[graph[tmp][i]]--;
if (into[graph[tmp][i]] == 0)
{
q.push(graph[tmp][i]);
}
ans[graph[tmp][i]] = (ans[graph[tmp][i]]+ans[tmp]) % N;
}
}
for (int i = 1; i <= n; i++)
{
if (!outto[i])
{
an = (an + ans[i]) % N;
}
}
cout << an;
return 0;
}
最后一题是我有点问题的,用aij很简单,所以我看了看动态规划的题解,发现有两个测试点过不了,所以发帖求助了,大家也可以找找有没有错:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
using namespace std;
int graph[1504][1504];
int dis[1504];
int n;
int dp(int x)
{
int ans = dis[x];
if (ans >= 0)
{
return ans;
}
if (n == x)
{
ans = 0;
dis[x] = ans;
return ans;
}
for (int i = 1; i <= n; i++)
{
if (graph[x][i] && dp(i) != -1)
ans = max(ans, dis[i] + graph[x][i]);
}
dis[x] = ans;
return ans;
}
int main()
{
memset(dis, -1, sizeof(dis));
int m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int a, b, w;
cin >> a >> b >> w;
graph[a][b] = max(graph[a][b], w);
}
cout << dp(1);
return 0;
}
打卡完毕!