补题补题补题

这篇博客探讨了图论中的关键算法,包括Dijkstra算法求解有向图中最短路径问题,以及在保证图连通性下最大化收益的最小生成树构造。文章通过实例分析了如何在删除边后重新计算最短路径,并介绍了如何使用优先队列优化Dijkstra算法。同时,还讲解了如何利用边跑最小生成树来寻找最大收益。
摘要由CSDN通过智能技术生成

Blocked Roads - AtCoder abc218_f - Virtual Judge

tags:dijkstra

题意:给你一个有向图,在去除一条边后,求源点到n的最短路

  • 2≤N≤400

  • 1≤M≤N(N−1)

TLE:m(o(n*n+m)超时

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#define x first
#define y second
using namespace std;
const int N=510;
typedef pair<int,int> PII;
map<int,PII> mp;
int dist[N];
bool st[N];
int g[N][N];
int n,m;

int dijkstra()
{
    memset(dist,0x3f,sizeof(dist));
    memset(st,0,sizeof(st));

    dist[1]=0;  

    for(int i=0;i<n;i++)      
    {
        int t=-1;      

        for(int j=1;j<=n;j++)
            if(!st[j]&&(t==-1||dist[t]>dist[j]))     
                t=j;

        st[t]=true;   

        for(int j=1;j<=n;j++)
		{
			dist[j]=min(dist[j],dist[t]+g[t][j]);
		}           
        
    }

    if(dist[n]==0x3f3f3f3f) return -1;
    return dist[n];
}
int main()
{
    cin>>n>>m;
    memset(g,0x3f,sizeof g); 
     for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x][y]=1;
        mp[i]={x,y};
    }
    
    for(int i=1;i<=m;i++)
    {
    	int x=mp[i].x,y=mp[i].y;
    	int z=g[x][y];
    	g[x][y]=0x3f3f3f3f;
    	cout<<dijkstra()<<endl;
    	g[x][y]=z;
	}
    return 0;
}

思路:记录最短路和最短路的路径,每次判断所连边是不是在最短路径中,在的话直接删边跑一遍dijkstra,否则直接输出最短路径

AC:时间复杂度:O(m*n)

#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(a) cout<<a<<'\n'
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=450,M=160005,inf=0x3f3f3f3f,mod=1e9+7;
vector<int> path[N]; 
int g[N][N];
int x[M],y[M];
int n,m;
priority_queue<PII,vector<PII>,greater<PII>> heap;

void dijkstra()
{
	int dist[N]={0};
	bool st[N]={0};
	for(int i=1;i<=n;i++) dist[i]=inf;
	heap.push({0,1});
	dist[1]=0;
	
	while(heap.size())
	{
		auto t=heap.top();
		heap.pop();
		
		int ver=t.second;
		if(st[ver]) continue;
		st[ver]=true;
		
		for(int i=1;i<=n;i++)
		{
			if(g[ver][i]==1)
			{
				if(dist[i]>dist[ver]+1)
				{
					dist[i]=dist[ver]+1;
					heap.push({dist[i],i}); 
				}
			}
		}
	}
	
	if(dist[n]==inf) cout<<-1<<endl;
	else cout<<dist[n]<<endl;
	 
}

int main()
{
	int dist[N]={0};
	bool st[N]={0};
    cin>>n>>m;
    memset(g,0x3f,sizeof(g));
    for(int i=1;i<=n;i++) dist[i]=inf;
    
    for(int i=1;i<=m;i++)
    {
    	cin>>x[i]>>y[i];
    	g[x[i]][y[i]]=1;
	}
    
    heap.push({0,1});
    dist[1]=0;
    
    while(heap.size())
    {
    	auto t=heap.top();
		heap.pop();
		
		int ver=t.second;
		if(st[ver]) continue;
		st[ver]=true;
		
		for(int i=1;i<=n;i++)
		{
			if(g[ver][i]==1)
			{
				if(dist[i]>dist[ver]+1)
				{
					path[ver].push_back(i);
					dist[i]=dist[ver]+1;
					heap.push({dist[i],i});
				}
			}
		}	
	}
	
	for(int i=1;i<=m;i++)
	{
		bool ok=false;
		
		for(auto k:path[x[i]])
		{
			if(k==y[i])
			{
				ok=true;
				g[x[i]][y[i]]=inf;
				dijkstra();
				g[x[i]][y[i]]=1;
			}
		}
		
		if(ok==false)
		{
			if(dist[n]==inf) cout<<-1<<endl;
			else cout<<dist[n]<<endl;
		}
		
	}
 
	return 0;
}

 Rectangles - AtCoder abc218_d - Virtual Judge

题意:给点,判断有多少个矩形

思路:map模拟

没有熟练掌握STL容器

AC

#include<bits/stdc++.h>
#define x first
#define mak make_pair
#define y second
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(a) cout<<a<<'\n'
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+10,M=1,inf=0x3f3f3f3f,mod=1e9+7;
int n,t;
map<PII,int> mp;
struct stu
{
	int x,y;
}p[2005];

int main()
{
    cin>>n;
    
    for(int i=1;i<=n;i++)
    {
    	cin>>p[i].x>>p[i].y;
    	mp[{p[i].x,p[i].y}]++;
	}
	
	int res=0;
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(p[i].x<p[j].x&&p[i].y<p[j].y)
			{
				if(mp.find(make_pair(p[i].x,p[j].y))!=mp.end()&&mp.find(make_pair(p[j].x,p[i].y))!=mp.end()) res++;
			}
		}
	}
	
	cout<<res<<endl;
	
	return 0;
}

Destruction - AtCoder abc218_e - Virtual Judge

tags:最小生成树

题意:

给出一个 n 个点 m 条边的无向连通图,每次可以移除一条边,收益为该边的权值 ci 。

问在保证图连通的情况下,最大收益为多少。

  • 2≤n≤2×1e5

  • n−1≤m≤2×1e5

  • −1e9≤ci≤1e9

思路:边跑最小生成树边判断两点是不是已经在最小生成树里面了,如果有,说明前面的权值一定是最小的,如果此时权值为正,则res加上

AC:

#include<bits/stdc++.h>
#define x first
#define y second
#define mak make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define debug(a) cout<<a<<'\n'
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=3e5+10,M=1,inf=0x3f3f3f3f,mod=1e9+7;
struct Edge
{
	int a,b,w; 
}edge[N];
int n,m;

bool cmp(Edge a,Edge b)
{
	return a.w<b.w;
}

int p[N];

int find(int x)
{
	if(p[x]!=x) p[x]=find(p[x]);
	return p[x]; 
}

void kur()
{
	sort(edge+1,edge+1+m,cmp);
	LL res=0;
	for(int i=1;i<=m;i++)
	{
		int a=edge[i].a;
		int b=edge[i].b;
		int w=edge[i].w;
		a=find(a),b=find(b);
		if(a!=b)
		{
			p[a]=b;
		}
		else if(w>0)//负权边只会让值变小
		{
			res+=w;
		}
	}
	
	cout<<res<<endl;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=m;i++)
    {
    	int a,b,w;
    	cin>>a>>b>>w;
    	edge[i]={a,b,w};
	}
	
	kur();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值