POJ3984 迷宫问题记录路径递归 bfs HDU1242 dfs Codeforces25D.Roads in Berland floyd优化 HDU1874畅通工程续 floyd/spfa/dj

POJ3984 迷宫问题记录路径递归 bfs HDU1242 dfs Codeforces25D.Roads in Berland floyd优化 HDU1874畅通工程续 floyd/spfa/dj_第1张图片

#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
ll n,sx,sy,ex,ey;//0:先序 1:镜像先序 
ll maze[5][5],vis[5][5];
ll dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

struct node{
	ll x,y;//d
};
node father[5][5];
bool judge(ll x,ll y){
	if(x>=0 && x<5 && y>=0 && y<5) return true;
	return false;
}
void bfs(ll sx,ll sy){//ll
	queue<node> q;
	q.push({sx,sy});//0
	vis[sx][sy]=1;

	while(!q.empty()){
		node now=q.front();
		q.pop();
		if(now.x==ex && now.y==ey){
			return;//return now.d;
		}
		for(int i=0;i<4;i++){
			node next;
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			if(!vis[next.x][next.y] && maze[next.x][next.y]!=1 && judge(next.x,next.y)){
				vis[next.x][next.y]=1;
			//	q.push({next.x,next.y,now.d+1});
				q.push(next);
				father[next.x][next.y]=now;
			}
		}
	}
}
void print(node cur){
	if(cur.x==0 && cur.y==0){
		printf("(%lld, %lld)\n",cur.x,cur.y);
		return;
	}
	print(father[cur.x][cur.y]);
	printf("(%lld, %lld)\n",cur.x,cur.y);
}
int main(){
	sx=0,sy=0,ex=4,ey=4;
	for(int i=0;i<5;i++){
		for(int j=0;j<5;j++){
			cin>>maze[i][j];
		}
	}
//	cout<
	bfs(sx,sy);
	print({ex,ey});
	return 0;
}

POJ3984 迷宫问题记录路径递归 bfs HDU1242 dfs Codeforces25D.Roads in Berland floyd优化 HDU1874畅通工程续 floyd/spfa/dj_第2张图片
#墙 .路 r朋友可以有多个 x与敌人战斗

#include
#include
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false);cin.tie(0);
const int maxn=2e5+5;
const int INF = 0x3f3f3f3f;
int n,m,time,minn,sx,sy,vis[205][205];
char s[205][205];

void dfs(int x,int y,int time){
	if(x<0 || x>=n || y<0 || y>=m) return;
	if(vis[x][y]) return;
	if(s[x][y]=='#') return;
	if(time>=minn) return;
	
	if(s[x][y]=='r'){
		if(time<minn) minn=time;return;
	}
	if(s[x][y]=='x') time++;
	vis[x][y]=1;
	dfs(x+1,y,time+1);
	dfs(x-1,y,time+1);
	dfs(x,y-1,time+1);
	dfs(x,y+1,time+1);
	vis[x][y]=0;
}

int main(){
	IO;
	while(cin>>n>>m){
		minn=INF;time=0;sx=0;sy=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>s[i][j];
				if(s[i][j]=='a') sx=i,sy=j;
			}
		
		}
		dfs(sx,sy,time);
		if(minn!=INF) printf("%d\n",minn);
		else printf("Poor ANGEL has to stay in the prison all his life.\n");
	}
	return 0;
}

枚举k 内层floyd O(n4) 优化u,v作为中间点
当改每一个 1<=i<=k边时 所有点对的最短路之和是多少 邻接矩阵的右上部分
D. Roads not only in Berland
给n*n 无向图邻接矩阵 n<=300
加k条边 u,v,val的每一条时 所有点对的最短路径之和是多少

2
0 5
5 0
1
1 2 3

3

3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1

17 12

1-2 4 1-3 5 2-3 9
加2-3为8的边 1-2 4 2-3 8 1-3 5
加1-2为1的边 1-2 1 2-3 6 1-3 5

#include
#include
#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false);cin.tie(0);
const int maxn=2e5+5;
const int INF = 0x3f3f3f3f;
ll g[305][305],n,k,u,v,val,sum,ans[305]; 
int main(){
	IO;
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>g[i][j];
		}
	}
	cin>>k;
	
	for(int t=1;t<=k;t++){
		cin>>u>>v>>val;
		if(val<g[u][v]){
			g[u][v]=g[v][u]=val;
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++)
					g[j][i]=g[i][j]=min(g[i][j],g[i][u]+g[u][j]);
			}
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					g[j][i]=g[i][j]=min(g[i][j],g[i][v]+g[v][j]);
				}
			}	
		}
		sum=0;
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++)
				sum+=g[i][j];
		}
		ans[t]=sum;
	}
	
	for(int i=1;i<=k;i++)	printf("%lld%c",ans[i],i==k?'\n':' ');
	return 0;
}

HDU1874
floyd邻接矩阵存 编号0-n-1

#include
using namespace std;
typedef long long ll;
#define IO ios::sync_with_stdio(false);cin.tie(0);
const int maxn=2e5+5;
const int INF = 0x3f3f3f3f;
ll g[205][205],n,m,minn,u,v,val;
void init(ll n){	//0-n-1
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++)
			g[i][j]=INF;
	}
	for(int i=0;i<n;i++){
		g[i][i]=0;
	}
}
void floyd(ll n){
	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
			}
		}
	}
}
int main(){
	IO;
	while(cin>>n>>m){
		init(n);
		for(int i=1;i<=m;i++){
			cin>>u>>v>>val;
			g[u][v]=min(g[u][v],val);
			g[v][u]=min(g[u][v],val);
		}
		floyd(n);
		cin>>u>>v;
		if(g[u][v]==INF)
			cout<<-1<<endl;
		else
			cout<<g[u][v]<<endl;
	}

	return 0;
}

你可能感兴趣的:(图论,dfs搜索)