最短路模板
话不多说,放代码:
#include <bits/stdc++.h>
using namespace std;
void SPFA_scan()
{
/*
int n,m;
bool vis[1000005];
struct edge{
int v,val;
};
vector<edge>dis[1000005];
int x,y,e;
int dp[1000005];
int tot[1000005];
queue<int>q;
*/
memset(dp,0x3f,sizeof(dp));
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&e);
edge now;
now.v=y;
now.val=e;
dis[x].push_back(now);
now.v=x;
now.val=e;
dis[y].push_back(now);
}
}
bool SPFA(int s,int t)
{
dp[s]=0;
tot[s]=1;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(int i=0;i<dis[now].size();i++)
{
int v=dis[now][i].v;
int w=dis[now][i].val;
if(dp[v]>dp[now]+w)
{
dp[v]=dp[now]+w;
if(!vis[v])
{
vis[v]=1;
tot[v]++;
q.push(v);
if(tot[v]>n)
{
return 0;
}
}
}
}
}
return 1;
}
void dijkstar_scan()
{
/*
int n,m;
int x,y;
bool vis[2505];
int k;
int e;
int s;int t;
int dp[2505];
struct ss{
int v;int val;
};
struct node{
int u;
int w;
bool operator<(const node x)const{return w>x.w;}
};
vector<ss>dis[2505];
priority_queue<node>q;
*/
memset(dp,0x3f,sizeof(dp));
scanf("%d %d %d %d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&e);
ss now;
now.val=e;
now.v=y;
dis[x].push_back(now);
now.v=x;
dis[y].push_back(now);
}
}
int dijkstra(int s,int t)
{
dp[s]=0;
vis[s] = 0;
node now;
now.u=s;
now.w=0;
q.push(now);
while(!q.empty())
{
node temp=q.top();
q.pop();
if(vis[temp.u])
{
continue;
}
vis[temp.u]=1;
int k=temp.u;
for(int i=0;i<dis[k].size();i++)
{
int l=dis[k][i].v;
if(dp[l]>dp[k]+dis[k][i].val)
{
dp[l]=dp[k]+dis[k][i].val;
node non;
non.u=l;
non.w=dp[l];
q.push(non);
}
}
}
return dp[t];
}
void print_dijkstra()
{
printf("%d",dijkstra(s,t));
}
void floyd_scan()
{
/*int n, m, ty;
int x, y;
int e;
int dis[505][505];
int pre[505][505];
int s, t;*/
memset(dis,0x3f,sizeof(dis));
scanf("%d %d",&n,&m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &x, &y, &e);
dis[x][y] = e;
dis[y][x] = e;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
{
pre[i][j]=0;
}
else
{
pre[i][j]=i;
}
}
dis[i][i]=0;
}
}
void print_floyd(int x)
{
if(pre[s][x]==0)
{
return;
}
print(pre[s][x]);
printf(" %d",x);
}
void floyd()
{
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dis[i][j] > dis[i][k] + dis[k][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
}
int main()
{
}