拓扑排序简介:https://blog.csdn.net/xxcdsg/article/details/127720727
#include
using namespace std;
const int MAXN = 1.5e3 + 10,MAXM = 5e4 + 10;
int a[MAXN] = {0,1};//记录距离1的层数
int son[MAXN];//记录入度
//构图
int ptop = 1;
struct link{
int next,val;
link* nex;
}p[MAXM];
link *head[MAXN];
void add(int x,int y,int val)
{
p[ptop].nex = head[x];
head[x] = &p[ptop];
p[ptop].next = y;
p[ptop].val = val;
ptop++;
}
//拓扑排序
queue qu;
//广搜
void dfs(int x)
{
link* pp = head[x];
while(pp != NULL)
{
int y = pp -> next,val = pp -> val;
son[y]--;
if(son[y] == 0)
qu.push(y);
if(a[x] != 0)
if(a[y] < a[x] + val)
a[y] = a[x] + val;//更新距离1的层数
pp = pp -> nex;
}
}
int main()
{
int n,m;cin >> n >> m;
for(int i = 1;i <= m;i++)
{
int x,y,val;scanf("%d %d %d",&x,&y,&val);
add(x,y,val);
son[y]++;
}
for(int i = 1;i <= n;i++)
if(son[i] == 0)
qu.push(i);
while(!qu.empty())
{
int x = qu.front();qu.pop();
dfs(x);
}
cout << a[n] - 1 << endl;
}
#include
using namespace std;
const int MAXN = 1e5 + 10,MAXM = 2e5 + 10;
struct node{
int dis,p;
bool operator <(const node &x)const{
return dis > x.dis;
}//重载运算符
};
priority_queue a;//存点
//构图
int ptop = 1;
struct link{
int next,val;
link* nex;
}p[MAXM];
link* head[MAXN];
void add(int x,int y,int val)
{
p[ptop].nex = head[x];
p[ptop].next = y;
p[ptop].val = val;
head[x] = &p[ptop];
ptop++;
}
int dis[MAXN];//距离答案
bool use[MAXN];
int main()
{
int n,m,s;cin >> n >> m >> s;
for(int i = 1;i <= m;i++)
{
int x,y,val;scanf("%d %d %d",&x,&y,&val);
add(x,y,val);
}
a.push((node){0,s});
while(!a.empty())
{
int predis = a.top().dis,po = a.top().p;a.pop();
if(use[po])
continue;
use[po] = 1;
dis[po] = predis;//确定这个点
link* pp = head[po];
while(pp != NULL)
{
int nextp = pp -> next,val = pp -> val;
if(!use[nextp] && (dis[nextp] == 0 || dis[nextp] >= predis + val))
{
dis[nextp] = predis + val;
a.push((node){dis[nextp],nextp});
}
pp = pp -> nex;
}
}
for(int i = 1;i <= n;i++)
cout << dis[i] << ' ';
}
#include
using namespace std;
const int MAXN = 1.5e3 + 10,MAXM = 5e4 + 10;
int a[MAXN][MAXN] = {0};
int main()
{
int n,m;cin >> n >> m;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
if(i == j);
else
a[i][j] = -1;
for(int i = 1;i <= m;i++)
{
int x,y,val;scanf("%d %d %d",&x,&y,&val);
if(a[x][y] < val)//存最长的
a[x][y] = val;
}
for(int k = 1;k <= n;k++)
for(int i = 1;i <= n;i++)
if(a[i][k] != -1)
for(int j = 1;j <= n;j++)
if(a[k][j] != -1)
{
a[i][j] = max(a[i][j],a[i][k] + a[k][j]);
}
cout << a[1][n];
}
#include
using namespace std;
const int MAXN = 1.5e3 + 10,MAXM = 5e4 + 10,INF = 0x3f3f3f3f;
//构图
struct link{
int nextp,val;
link* nex;
}p[MAXM];
link *head[MAXN];
int ptop = 1;
void add(int x,int y,int val)
{
p[ptop].nex = head[x];
head[x] = &p[ptop];
p[ptop].nextp = y;
p[ptop].val = val;
ptop++;
}
int dis[MAXN] = {0};
void Bellman_ford(int n)
{
for(int i = 2;i <= n;i++)
{
dis[i] = INF;//不连通
}
for(int i = 1;i < n;i++)
{
for(int j = 1;j <= n;j++)
{
if(dis[j] == INF)
continue;
link* pp = head[j];
while(pp != NULL)
{
int y = pp -> nextp,val = pp -> val;
if(dis[y] == INF)
dis[y] = dis[j] + val;
else if(dis[y] < dis[j] + val)
dis[y] = dis[j] + val;
pp = pp -> nex;
}
}
}
}
int main()
{
int n,m;cin >> n >> m;
for(int i = 1;i <= m;i++)
{
int x,y,val;scanf("%d %d %d",&x,&y,&val);
add(x,y,val);
}
Bellman_ford(n);
if(dis[n] == INF)
cout << -1;
else
cout << dis[n];
}
#include
using namespace std;
const int MAXN = 1.5e3 + 10,MAXM = 5e4 + 10,INF = 0x3f3f3f3f;
//构图
struct link{
int nextp,val;
link* nex;
}p[MAXM];
link *head[MAXN];
int ptop = 1;
void add(int x,int y,int val)
{
p[ptop].nex = head[x];
head[x] = &p[ptop];
p[ptop].nextp = y;
p[ptop].val = val;
ptop++;
}
int dis[MAXN] = {0};
void SPFA(int n)
{
for(int i = 2;i <= n;i++)//除起点距离均为INF
dis[i] = INF;
queue qu;
qu.push(1);//起点入队
while(!qu.empty())
{
int x = qu.front();qu.pop();
link* pp = head[x];
if(dis[x] != INF)
while(pp != NULL)
{
int y = pp -> nextp,val = pp -> val;
if(dis[y] == INF || dis[y] < dis[x] + val)
{
qu.push(y);
dis[y] = dis[x] + val;
}
pp = pp -> nex;
}
}
}
int main()
{
int n,m;cin >> n >> m;
for(int i = 1;i <= m;i++)
{
int x,y,val;scanf("%d %d %d",&x,&y,&val);
add(x,y,val);
}
SPFA(n);
if(dis[n] == INF)
cout << -1;
else
cout << dis[n];
}
void SPFA(int n)
{
for(int i = 2;i <= n;i++)//除起点距离均为INF
dis[i] = INF;
deque qu;//双端队列
qu.push_front(1);//起点入队
while(!qu.empty())
{
int x = qu.front();qu.pop_front();
link* pp = head[x];
if(dis[x] != INF)
while(pp != NULL)
{
int y = pp -> nextp,val = pp -> val;
if(dis[y] == INF || dis[y] < dis[x] + val)
{
dis[y] = dis[x] + val;
if(qu.empty())//如果为空,直接放
qu.push_front(y);
else if(dis[qu.front()] < dis[y])//如果比较大,放队首
qu.push_front(y);
else//比较小放队尾
qu.push_back(y);
}
pp = pp -> nex;
}
}
}
void SPFA(int n)
{
int sum = 0,num = 1;
for(int i = 2;i <= n;i++)//除起点距离均为INF
dis[i] = INF;
queue qu;
qu.push(1);//起点入队
while(!qu.empty())
{
int x = qu.front();
while(dis[x] * num < sum)//核心 小于平均值
{
qu.pop();
qu.push(x);
x = qu.front();
}
qu.pop();
sum -= dis[x];
num--;
link* pp = head[x];
while(pp != NULL)
{
int y = pp -> nextp,val = pp -> val;
if(dis[y] == INF || dis[y] < dis[x] + val)
{
dis[y] = dis[x] + val;
qu.push(y);
sum += dis[y];
num++;
}
pp = pp -> nex;
}
}
}
void SPFA(int n)
{
for(int i = 2;i <= n;i++)//除起点距离均为INF
dis[i] = INF;
deque qu;//双端队列
int sum = 0,num = 1;
qu.push_front(1);//起点入队
while(!qu.empty())
{
int x = qu.front();
while(dis[x] * num < sum)//核心 小于平均值
{
qu.pop_front();
qu.push_back(x);
x = qu.front();
}
qu.pop_front();
sum -= dis[x];
num--;
link* pp = head[x];
if(dis[x] != INF)
while(pp != NULL)
{
int y = pp -> nextp,val = pp -> val;
if(dis[y] == INF || dis[y] < dis[x] + val)
{
dis[y] = dis[x] + val;
sum += dis[y];
num++;
if(qu.empty())//如果为空,直接放
qu.push_front(y);
else if(dis[qu.front()] < dis[y])//如果比较大,放队首
qu.push_front(y);
else//比较小放队尾
qu.push_back(y);
}
pp = pp -> nex;
}
}
}