1 spfa虽然已经死了,可是他还有一个作用就是判断负环和正环(如果在一个点跑了大于n次那么这个图就存在负环)正环跑最长路,负环跑最短路
/*
I am nothing but I must be everything.
*/
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll maxn = 2005;
typedef pair<ll,ll> p;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
ll t;
cin >> t;
while(t--)
{
vector<p>a[maxn];
ll dis[maxn],n,m,sum[maxn] = {0};
queue<ll>q;
cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
ll x,y,z;
cin >> x >> y >> z;
if(z >= 0)
{
a[x].push_back(make_pair(y,z));
a[y].push_back(make_pair(x,z));
}
else
{
a[x].push_back(make_pair(y,z));
}
}
memset(dis, 0x3f, sizeof(dis));
ll flag[maxn] = {0};
while(!q.empty())q.pop();
q.push(1);
dis[1] = 0;
flag[1] = 1;
sum[1]++;
while(!q.empty())
{
ll k = q.front();
q.pop();
flag[k] = 0;
//if(dis[en] < be)continue;
for(int i = 0; i < a[k].size(); i ++)
{
ll to = a[k][i].first;
ll wei = a[k][i].second;
if(dis[to] > dis[k] + wei)
{
dis[to] = dis[k] + wei;
if(!flag[to])
{
flag[to] = 1;
q.push(to);
sum[to] ++;
}
if(sum[k] > n){cout << "YE5" << endl;goto l1;}
}
}
}
cout << "N0" << endl;
l1 :;
}
return 0;
}
2 堆优化的dij,相当于每次找到那个最小值全是用堆这样时间复杂度就变成了log2n;那么总的时间复杂度就是(n + m)log2n;
#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#include <utility>
#include <cstdio>
#define maxn 200005
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll n,m,k;
ll dis1[maxn],dis2[maxn];
bool flag[maxn];
struct node
{
ll x;
ll d;
node(ll xx,ll dd):x(xx),d(dd){};
};
bool operator < (const node a,const node b)
{
return a.d > b.d;
}
vector< pair<ll,ll> > p[maxn];
void dij(ll k)
{
priority_queue<node>q;
fill(dis1,dis1+n+1,inf);
fill(dis2,dis2+n+1,inf);
dis1[k] = 0;
q.push(node(1,0));
while(!q.empty())
{
node now = q.top();
q.pop();
if(flag[now.x])continue;
flag[now.x] = 1;
for(int i = 0; i < p[now.x].size(); i ++)
{
ll v = p[now.x][i].first;
ll w = p[now.x][i].second;
ll d = dis1[now.x] + w;
if(dis1[v] > d && !flag[v])
{
dis1[v] = d;
q.push(node(v,dis1[v]));
}
// if(dis2[v] > d && dis1[v] < d)
// {
// dis2[v] = d;
// q.push(node(v,dis2[v]));
// }
}
}
//printf("%lld\n",dis2[n]);
}
int main()
{
scanf("%lld%lld%lld",&n,&m,&k);
for(int i = 1; i <= m; i ++)
{
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
p[a].push_back(make_pair(b,c));
//p[b].push_back(make_pair(a,c));
}
dij(k);
for(int i = 1; i <= n; i ++)
{
cout << dis1[i] << " ";
}
cout << endl;
return 0;
}
堆优化的prim算法(和dij非常相似这里不做过多解释)
/*
I am nothing but I must be everything.
*/
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const ll maxn = 1e5 + 5;
typedef pair<ll,ll> p;
vector<p>a[maxn];
ll dis[maxn],n,m;
ll prim()
{
ll ans = 0;
bool flag[maxn] = {0};
memset(dis + 1, 0x3f, n * sizeof(dis[1]));
priority_queue<p,vector<p>,greater<p> >q;
q.push(p(0,1));
dis[1] = 0;
while(!q.empty())
{
p k = q.top();
q.pop();
ll be = k.first;
ll en = k.second;
flag[en] = true;
if(dis[en] < be)continue;
for(int i = 0; i < a[en].size(); i ++)
{
ll to = a[en][i].first;
ll wei = a[en][i].second;
if(!flag[to] && dis[to] > wei)
{
dis[to] = wei;
q.push(p(dis[to],to));
}
}
ans += dis[en];
}
return ans;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= m; i ++)
{
ll x,y,z;
cin >> x >> y >> z;
a[x].push_back(make_pair(y,z));
a[y].push_back(make_pair(x,z));
}
cout << prim() << endl;
return 0;
}