题目链接:
https://www.luogu.org/problem/P2136
思路提示博客:https://www.luogu.org/blog/user27165/solution-p2136
思路:
1:关键点:“拉近距离”的不一定是小明,也可能是小红,题目中没有给出具体的源点汇点,所以在做spfa的时候,分别以1和n为源点各做一遍,取min
spfa(1);
int mi=d[n];
spfa(n);
cout<<min(mi,d[1])<<endl;
2:判断负环的方法,cnt数组,存一个节点入队的次数,如果大于n则说明存在负环
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+1;
vector<pair<int,int> >e[maxn];
int n,m,ing[maxn],a,b,c,d[maxn],cnt[maxn];
inline void spfa(int s)
{
memset(ing,0,sizeof(ing));
memset(d,0x7f,sizeof(d));
queue<int>q;
q.push(