图论算法汇总

拓扑排序

#include<bits/stdc++.h>
#define inf 0x7fffffff
#define LL long long
#define rs register
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
const int maxn=105;
int mp[maxn][maxn],indegree[maxn],topo[maxn];
int n; stack<int>s;
void TopoSort(){
	LL cnt=0;
    for(int i=1;i<=n;i++){
        if(indegree[i]==0){s.push(i);}
	}
    while(!s.empty()){
		int u=s.top();
    	s.pop();
    	topo[++cnt]=u;
    	for(int j=1;j<=n;j++){
            if(mp[u][j])
            	if(--indegree[j]==0)
        			s.push(j);
		}
	}
}

int main(){
    cin>>n;
	for(int i=1;i<=n;i++){
    	LL v;
    	while(cin>>v&&v){
    		mp[i][v]=1;
    		indegree[v]++;
		}
	}
	TopoSort();
	for(int i=1;i<n;i++){cout<<topo[i]<<" ";}
	cout<<topo[n]<<endl;
    return 0;
}

生成树算法

Kruskal 最小生成树算法

//无向图-边集数组
#include<bits/stdc++.h>
#define inf 0x7fffffff
#define LL long long
#define rs register
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
LL fa[1000010],n,m;
struct Edge{LL u,v,w;}a[10000010];
bool cmp(Edge x,Edge y){
    return x.w<y.w;
}
void Init(LL n){
    for(LL i=1;i<=n;i++)
        fa[i]=i;
}
LL Find(LL x){
    if(x!=fa[x])
	fa[x]=Find(fa[x]);
    return fa[x];
}
bool Merge(LL a,LL b){
    LL p=Find(a);
    LL q=Find(b);
    if(p==q) return 0;
    fa[q]=p;
    return 1;
}
LL Kruskal(LL n){
    LL ans=0;
    sort(a,a+m*2,cmp);
    for(LL i=0;i<m*2;i++)
        if(Merge(a[i].u,a[i].v)){
            ans+=a[i].w;
            n--;
            if(n==1)
                return ans;
        }
    return -1;
}
int main(){
    fastOI; cin>>n>>m;
    Init(n);
    for(LL i=1;i<=m;i++){
        cin>>a[i].u>>a[i].v>>a[i].w;
        a[i+m]=a[i];
    }
    LL res=Kruskal(n);
    if(res==-1){cout<<"No way."<<endl;}
    else cout<<res<<endl;
    return 0;
}

最短路算法

Dijsktra 最短路算法


写法一:无返回值函数
//有向图-链式前向星
#include<bits/stdc++.h>
#define inf 0x7fffffff
#define LL long long
#define rs register
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
LL n,m,s,head[1000010],dis[10000010],con=0;
struct graph{ LL v,w,nxt; }a[1000010];
struct node{
	LL u,d;
	bool operator <(const node& nx)const{
		return d>nx.d;
	}
};
void add(LL u,LL v,LL w){
	a[++con].v=v;
	a[con].w=w;
	a[con].nxt=head[u];
	head[u]=con;
}
void dijkstra(){
	for(rs LL i=1;i<=n;i++){dis[i]=inf;}
	dis[s]=0;
	priority_queue<node>q;
	q.push((node){s,0});
	while(!q.empty()){
		node fro=q.top(); q.pop();
		auto u=fro.u,d=fro.d;
		if(d!=dis[u])continue;
		for(rs LL i=head[u];i;i=a[i].nxt){
			auto v=a[i].v,w=a[i].w;
			if(dis[u]+w<dis[v]){
				dis[v]=dis[u]+w;
				q.push((node){v,dis[v]});
			}
		}
	}
}
int main(){
	fastOI; cin>>n>>m>>s;
	for(rs LL i=1;i<=m;i++){
		LL u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	dijkstra();
	for(LL i=1;i<=n;i++){cout<<dis[i]<<" ";}
	return 0x0;
}

写法二:有返回值函数
//链式前向星 - 有向图 (取消注释 49 行转化为 无向图)
//求 st ----> en 的最短路
#include<bits/stdc++.h>
#define inf 0x7fffffff
#define LL long long
#define rs 
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
LL n,m,st,en,head[1000010],dis[10000010],con=0;
struct graph{ LL v,w,nxt; }a[1000010];
struct node{
    LL u,d;
    bool operator <(const node& nx)const{
        return d>nx.d;
    }
};
void add(LL u,LL v,LL w){
    a[++con].v=v;
    a[con].w=w;
    a[con].nxt=head[u];
    head[u]=con;
}
LL dijkstra(LL st,LL en){
    memset(dis,0x3f,sizeof(dis));
    dis[st]=0;
    priority_queue<node>q;
    q.push((node){st,0});
    while(!q.empty()){
        node fro=q.top(); q.pop();
        auto u=fro.u,d=fro.d;
        if(d!=dis[u])continue;
        for(rs LL i=head[u];i;i=a[i].nxt)
        {
            auto v=a[i].v,w=a[i].w;
            if(dis[u]+w<dis[v]){
                dis[v]=dis[u]+w;
                q.push((node){v,dis[v]});
            }
        }
    }
	return dis[en];
}
int main(){
    fastOI; cin>>m>>n>>st>>en;
    for(rs LL i=1;i<=m;i++){
        LL u,v,w;
        cin>>u>>v>>w;
        add(u,v,w);
		//add(v,u,w);
		//去掉注释转为无向图
    }
	cout<<dijkstra(st,en)<<endl;
    return 0x0;
}


Floyd 最短路算法

//无向图-邻接矩阵
#include<bits/stdc++.h>
#define inf 0x7fffffff
#define LL long long
#define rs register
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
LL n,m,a[1010][1010];
int main(){
	fastOI; cin>>n>>m;
	for(LL i=1;i<=n;i++)
	for(LL j=1;j<=n;j++){a[i][j]=inf;}
	for(LL i=1;i<=n;i++){a[i][i]=0;}
	for(LL i=1;i<=m;i++){
		LL u,v,w; cin>>u>>v>>w;
		a[u][v]=min(a[u][v],w);
		//a[v][u]=min(a[v][u],w);
	    //去掉注释转为无向图
	}
	for(LL k=1;k<=n;k++)
	for(LL i=1;i<=n;i++)
	for(LL j=1;j<=n;j++){
		a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
	}
	for(LL i=1;i<=n;i++){
		for(LL j=1;j<=n;j++){
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0x0;
}

SPFA 最短路算法(负权图)


// SPFA - 有向图 链式前向星
#include<bits/stdc++.h>
#define inf 2147483647;
#define LL long long
#define rs register
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
const LL maxn=10010,maxe=1000005;
LL n,m,s,cnt=0;
LL head[maxn],dis[maxn],sum[maxn];
bool vis[maxn];
struct node{
	LL to,next,w;
}e[maxe];

void add(LL u,LL v,LL w){
	e[cnt].to=v;
	e[cnt].next=head[u];
	e[cnt].w=w;	
	head[u]=cnt++;
}

bool spfa(LL u){
	queue<LL>q;
	memset(vis,0,sizeof(vis));
	memset(sum,0,sizeof(sum));
	memset(dis,0x3f,sizeof(dis));
	vis[u]=1; dis[u]=0; sum[u]++;
	q.push(u);
	while(!q.empty()){
		LL x=q.front();
		q.pop();
		vis[x]=0;
		for(LL i=head[x];~i;i=e[i].next){
			LL v=e[i].to;
			if(dis[v]>dis[x]+e[i].w){
				dis[v]=dis[x]+e[i].w;
				if(!vis[v]){
					if(++sum[v]>=n){return 1;}
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	return 0;
}

int main(){
    //freopen("spfa.in","r",stdin); freopen("spfa.out","w",stdout);
	fastOI; cnt=0; cin>>n>>m>>s;
	memset(head,-1,sizeof(head));
	LL u,v,w;
	for(LL i=1;i<=m;i++){
		cin>>u>>v>>w;
		add(u,v,w);
        //add(v,u,w);
        //去掉注释转为无向图
	}
	if(spfa(s)){cout<<"Impossible"<<endl;}
	else{
        for(LL pot=1;pot<=n;pot++){
            cout<<dis[pot]<<endl;
            //cout<<min(dis[pot],2147483647ll)<<" ";
            //此处注意不可达的距离是 2147483647 (题目所给)
        }
	    cout<<endl;
    }
	return 0;
}

Bellman Ford 最短路算法(谨慎使用)


#include<bits/stdc++.h>
#define inf 0x7fffffff
#define LL long long
#define rs register
#define fastOI ios::sync_with_stdio(0)
#define fo(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
struct node{
	LL a,b,w;
}e[100010];

LL n,m,cnt=0,s,dis[100010];
LL a,b,w;
void add(LL a,LL b,LL w){
	e[cnt].a=a;
	e[cnt].b=b;
	e[cnt++].w=w;
}

bool bellman_ford(LL u){
	memset(dis,0x3f,sizeof(dis));
	dis[u]=0;
	for(LL i=1;i<n;i++){
		bool flag=0;
		for(LL j=0;j<m;j++)
			if(dis[e[j].b]>dis[e[j].a]+e[j].w){
				dis[e[j].b]=dis[e[j].a]+e[j].w;
				flag=1;
			}
		if(!flag)return 0;
	}
	for(LL j=0;j<m;j++)
		if(dis[e[j].b]>dis[e[j].a]+e[j].w)
			return 1;
	return 0;
}

void print(){
	for(LL i=1;i<=n;i++)
		cout<<dis[i]<<" ";
	cout<<endl;
}

int main(){
	cin>>n>>m>>s;
	for(LL i=0;i<m;i++){
		cin>>a>>b>>w;
		add(a,b,w);
	}
	if(bellman_ford(s))
		cout<<"IMpossible"<<endl;
	else print();	
	return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值