下面分享3道简单题主要是熟悉spfa,和在求负环上的应用。
题一:洛谷P3385 【模板】负环
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 2010, M = 2*3010;
int h[M], ne[M], e[M], w[M];
int cnt[N];
int d[N];
bool st[N];
int n, m;
int t;
int idx;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
memset(d, 0x3f, sizeof(d));
memset(st, false, sizeof(st));
memset(cnt, 0, sizeof(cnt));
queue<int >q;
/*for (int i = 1;i <= n;i++) {
q.push(i);
st[i] = true;
}*/
q.push(1);
st[1]=true;
d[1]=0;
while (q.size())
{
int o = q.front();
q.pop();
st[o] = false;
for (int i = h[o];i != -1;i = ne[i]) {
int j = e[i];
if (d[j] > d[o] + w[i]) {
d[j] = d[o] + w[i];
cnt[j] = cnt[o] + 1;
if (cnt[j] == n) {
return true;
}
if (st[j]) {
continue;
}
q.push(j);
st[j] = true;
}
}
}
return false;
}
int main()
{
scanf("%d", &t);
while (t--) {
memset(h, -1, sizeof(h));
idx = 0;
scanf("%d%d", &n, &m);
for (int i = 0;i < m;i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (c >= 0) {
add(a, b, c);
add(b, a, c);
}
else {
add(a, b, c);
}
}
if (spfa()) {
printf("YES\n");
}
else {
printf("NO\n");
}
}
}
这题没有特值,不需要考虑能否从1到n。
题二:洛谷P2850 [USACO06DEC] Wormholes G(虫洞)
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 510, M = 5500;
int h[M], ne[M], e[M], w[M];
int d[N];
int cnt[N];
bool st[N];
int idx, n, m, t;
int w1;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
memset(st, false, sizeof(st));
memset(d, 0, sizeof(d));
memset(cnt, 0, sizeof(cnt));
queue <int>q;
for (int i = 1;i <= n;i++) {
q.push(i);
st[i] = true;
}
while (q.size()) {
int o = q.front();
q.pop();
st[o] = false;
for (int i = h[o];i != -1;i = ne[i]) {
int j = e[i];
if (d[j] > d[o] + w[i]) {
d[j] = d[o] + w[i];
cnt[j] = cnt[o] + 1;
if(cnt[j] == n) {
return true;
}
if (st[j]) {
continue;
}
q.push(j);
st[j] = true;
}
}
}
return false;
}
int main()
{
scanf("%d", &t);
while (t--)
{
memset(h, -1, sizeof(h));
idx = 0;
scanf("%d%d%d", &n, &m,&w1);
for (int i = 0;i < m;i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
for (int i = 0;i < w1;i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, -c);
}
if (spfa())
{
printf("YES\n");
}
else {
printf("NO\n");
}
}
}
这题存在特值,即前面的点可能不会走向虫洞,需要引入一个源点,将d初始化为0,即所有点都将以0为权联通,这样就一定会走向虫洞。
题三:洛谷P2136 拉近距离
这题同样特值,需要考虑小明和小红两个人分别用spfa,(爱情是两个人的事),没有考虑到这一点,就只有90分,(正所谓只有一个人付出的爱情不会圆满)。
感觉这题还是相当有意思,充满哲理。
好了为了小明和小红的forever love,开始练习吧。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 10010;
int h[M], e[M], ne[M], w[M];
int d[N];
int cnt[N];
bool st[N];
int n, m, idx;
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
memset(d, 0x3f, sizeof(d));
memset(st, false, sizeof(st));
memset(cnt, 0, sizeof(cnt));
queue<int> q;
q.push(n);
st[n] = true;
d[n] = 0;
while (q.size())
{
int o = q.front();
q.pop();
st[o] = false;
for (int i = h[o];i != -1;i = ne[i]) {
int j = e[i];
if (d[j] > d[o] + w[i])
{
d[j] = d[o] + w[i];
cnt[j] = cnt[o] + 1;
if (cnt[j] == n) {
return true;
}
if (st[j]) {
continue;
}
q.push(j);
st[j] = true;
}
}
}
return false;
}
bool spfa2()
{
memset(d, 0x3f, sizeof(d));
queue<int> q;
q.push(1);
st[1] = true;
d[1] = 0;
while (q.size())
{
int o = q.front();
q.pop();
st[o] = false;
for (int i = h[o];i != -1;i = ne[i]) {
int j = e[i];
if (d[j] > d[o] + w[i])
{
d[j] = d[o] + w[i];
cnt[j] = cnt[o] + 1;
if (cnt[j] == n) {
return true;
}
if (st[j]) {
continue;
}
q.push(j);
st[j] = true;
}
}
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof(h));
for (int i = 0;i < m;i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, -c);
}
if (spfa2()||spfa()) {
printf("Forever love");
}
else {
spfa();
int s = d[1];
spfa2();
int s1 = d[n];
printf("%d", min(s,s1));
}
}