最短(长)路径(易忘)。
v
infinity°º
stay hungry stay foolish
展开
-
Floyd中间点k必须放最外层
PTA | 程序设计类实验辅助教学平台#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=515,inf=0x3f3f3f3f;int n;ll g[N][N];char c;vector<int>f,m;struct nn{ int i; ll v; friend bool operator<(nn a,nn b){ .原创 2022-04-30 14:59:06 · 135 阅读 · 0 评论 -
天梯L2-001 紧急救援 (纯dij不太熟)
PTA | 程序设计类实验辅助教学平台有空优先队列试一下。#include<bits/stdc++.h>using namespace std;const int N=510;const int inf=0x3f3f3f3f;typedef long long ll;int n,m,s,t,a[N],vis[N],num[N],dis[N],pre[N],road[N],g[N][N];//N<1000用矩阵可以过,以后优先用矩阵。前向星在这题会超时!搭配优先..原创 2021-12-12 22:48:24 · 159 阅读 · 0 评论 -
作物杂交(蓝桥最短路):bfs优先队列(矩阵/链式前向星)/dfs
和普通最短路的区别,多了一个中间值。即不是直接u->v,而是u,t->v优先队列+矩阵#include <bits/stdc++.h>using namespace std;const int maxn=2e3+5;const int inf=0x3f3f3f3f;int n,m,k,t,a[maxn],dis[maxn],vis[maxn],g[maxn][maxn],x,y,c;struct nn{ int v,t; friend bool .原创 2021-11-10 21:34:47 · 131 阅读 · 0 评论 -
hdu1317spfa最长路
#include<bits/stdc++.h>using namespace std;const int maxn=110;const int inf=0x3f3f3f3f;int n,m;int d[maxn],head[maxn],inq[maxn],cnt[maxn],tot;//d[i]:i点的值。inq[i]:i是否在队列里。cnt[i]:i访问的次数struct edge{int to,w,nex;}e[maxn*maxn];void add(int u,int v.原创 2021-09-22 20:38:35 · 59 阅读 · 0 评论 -
hdu2544基于堆优化(优先队列)的最短路径。变简单了
#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;const int maxn=105;int n,m,head[maxn],cnt,vis[maxn],dis[maxn];struct node{ int id,d; friend bool operator < (node a,node b) { return a.d<b.d; }};struct edge{ int to,.原创 2021-08-01 11:05:58 · 69 阅读 · 0 评论 -
hdu3592 差分约束总结spfa
-1没有 -2无限多个d[i]为第i个人的位置,d[n]-d[1]是1~n间的距离#include<bits/stdc++.h>#define inf 0x3f3f3f3ftypedef long long ll;using namespace std;const int maxn=1e3+5;int n,cnt;int head[maxn],vis[maxn],dis[maxn],used[maxn];struct edge{ int v,w,nex;}e[max..原创 2021-08-18 14:41:26 · 77 阅读 · 0 评论 -
hdu1384差分约束spfa BF优先队列优化。可解决负权负环
d[i] 表示区间 [0,i] 中包含的集合中数字的个数。区间 [a,b] 中包含的集合中数字的个数: d[b] - d[a-1]>=c, 求最长路径隐含条件:d[i] - d[i-1] >= 0;d[i] - d[i-1] <= 1符号不一致的,转成负边d[i-1] - d[i] >= -1再建边d[b] - d[a-1]所以是建a-1指向b(被减数)的边增加虚点 s ,与所有点相连,边权为0。是为了防止图不连通。由于上面的隐含条件,此题就不需要增加了,但不增加的话需要原创 2021-08-18 12:42:12 · 120 阅读 · 0 评论 -
hdu1217Floyd求任意两点间距离,可处理负权
#include<bits/stdc++.h>using namespace std;int n,m;map<string ,int> mp;double g[35][35];int main(){ string a,b; int cas=0; double x; while(cin>>n,n!=0){ for(int i=1;i<=n;i++){ cin>>a; mp[a]=i; } for(int i=1;.原创 2021-08-17 22:56:45 · 62 阅读 · 0 评论 -
hdu1869六度分离(floyd法)
#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;int n,m;int dis[110][110];int 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]=di.原创 2021-08-17 19:02:30 · 74 阅读 · 0 评论 -
hdu2544最短路dijkstra优先队列优化模板
//无向图,边数要乘2//friend bool 返回的 相反#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;const int maxn=1e4;int n,m,cnt,s;int head[105],dis[105],vis[105];struct edge{ int to,dis,nex;}e[2*maxn+5];//无向图,边数要乘2 void adde(int u,in...原创 2021-08-17 17:56:31 · 101 阅读 · 0 评论 -
poj3984 CE
#include<bits/stdc++.h>using namespace std;const int maxn=5;int n,g[maxn][maxn],vis[maxn][maxn];int dir[4][2]={1,0,0,-1,-1,0,0,1};struct node{ int x,y;}pre[maxn][maxn];void printt(){ stack<node> s; node cnt={4,4}; while(1){ s.push.原创 2021-05-30 15:08:01 · 55 阅读 · 0 评论 -
hdu2680dijks 法1普通矩阵 法2优先队列+链式前向星
#include<bits/stdc++.h>using namespace std;#define LOCALconst int inf=0x3f3f3f3f;const int maxn=1e3+5;int n,m,vis[maxn],dir[maxn],g[maxn][maxn];int main(){ #ifdef LOCAL freopen(".\\in.txt","r",stdin); freopen(".\\out.txt","w",stdout); #end.原创 2021-05-25 20:51:09 · 127 阅读 · 0 评论 -
hdu2066
#include<bits/stdc++.h>using namespace std;const int inf=0x3f3f3f3f;const int maxn=1e3+5;int g[maxn][maxn],vis[maxn],dir[maxn];int main(){ int T,S,D,a,b,x,s,t; while(~scanf("%d%d%d",&T,&S,&D)){ memset(vis,1,sizeof(vis)); memse.原创 2021-05-23 14:48:59 · 56 阅读 · 0 评论 -
hdu1874最短路径(易忘)dijks
#include<bits/stdc++.h>using namespace std;const int maxn=205;const int inf=0x3f3f3f3f;int n,m,g[maxn][maxn],vis[maxn],dis[maxn];int main(){ int a,b,x,s,t,minn,nex; while(cin>>n>>m){ for(int i=0;i<n;i++){ dis[i]=inf;vis[i].原创 2021-05-18 20:47:46 · 89 阅读 · 0 评论