当前位置: 首页 > 工具软件 > Maelstrom > 使用案例 >

POJ 1502 MPI Maelstrom 解题报告

慕晨
2023-12-01

Question link

Dijkstra

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 109
int e[N][N];
int atoi(char* s){
	int sum = 0; 	
	for (int i = 0; i <= strlen(s) - 1; i++){
		sum = sum*10 + s[i] - '0';
	}
	return sum; 
}
int dis[N];bool book[N];int n;
void dijkstra(){
	rep(i,1,n)	dis[i] = inf; 
	mset(book,0);
	dis[1] = 0;
	int minx = inf; int u;  
	for (int ai = 1; ai <= n; ai++){
		minx = inf; 
		for (int bi = 1; bi <= n; bi++){
			if ( minx > dis[bi] && book[bi] == 0 ){
				minx = dis[bi],u = bi; 
			}
		}
		book[u] = 1; 
		for (int bi = 1; bi <= n; bi++){
			if( book[bi] == 0 && dis[bi] > dis[u] + e[u][bi] && e[u][bi] != inf)
				dis[bi] = dis[u] + e[u][bi];
		}
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
	 
	n = read();
	rep(i,1,n)	rep(j,1,n)	e[i][j] = inf; 
	rep(i,1,n)	e[i][i] = 0; 
	char s[20];
	for(int i=2;i<=n;i++)
	for(int j=1;j<i;j++)
	{
		scanf("%s",s);
		if(s[0]!='x')
			e[i][j]=e[j][i]=atoi(s);	//将字符串转换为数字
	}	
	dijkstra();
	int ans = -inf ; 
	for (int i = 2; i <= n; i++){
		if ( ans < dis[i] )
			ans = dis[i] ;
	}
	cout<<ans;
	return 0;
}

Bellman-Ford

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 109
struct node {
	int u,v,w,nx; 
}e[N*N];int tot; int hea[N];
int atoi(char* s){
	int sum = 0; 	
	for (int i = 0; i <= strlen(s) - 1; i++){
		sum = sum*10 + s[i] - '0';
	}
	return sum; 
}
int dis[N];bool book[N];int n;
void add_ead(int u,int v,int w){
	e[++tot].u = u; e[tot].v = v; e[tot].w = w; e[tot].nx = hea[u]; hea[u] = tot; 
}

void SPFA(){
	for (int i = 1; i <= n; i++)	dis[i] = inf; 
	dis[ 1 ] = 0; 
	mset(book,0);
	queue< int > q; 
	q.push( 1 ); book[1] = 1; 
	while ( !q.empty() ){
		int u = q.front() ; 
		q.pop();
		book[u] = 0; 
		for (int i = hea[u]; i ; i = e[i].nx ){
			if ( dis[e[i].v] > dis[e[i].u] + e[i].w){
				dis[ e[i].v ] = dis[e[i].u] + e[i].w; 
				if ( !book[ e[i].v ] ){
					q.push(e[i].v);
					book[ e[i].v ] = 1; 
				}
			}
		}
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
	 
	n = read();
	char s[20];
	for(int i=2;i<=n;i++)
	for(int j=1;j<i;j++)
	{
		scanf("%s",s);
		if(s[0]!='x'){
			add_ead(i,j,atoi(s));
			add_ead(j,i,atoi(s));
		}
	}	
	SPFA();
	int ans = -inf ; 
	for (int i = 2; i <= n; i++){
		if ( ans < dis[i] )
			ans = dis[i] ;
	}
	cout<<ans;
	return 0;
}

一定会有出队,vis[ u ] == 0 ,因为,会被再次更新。

Dijkstra adjacency list

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 109
struct node {
	int u,v,w,nx; 
}e[N*N];int tot; int hea[N];
int atoi(char* s){
	int sum = 0; 	
	for (int i = 0; i <= strlen(s) - 1; i++){
		sum = sum*10 + s[i] - '0';
	}
	return sum; 
}
int dis[N];bool book[N];int n;
void add_ead(int u,int v,int w){
	e[++tot].u = u; e[tot].v = v; e[tot].w = w; e[tot].nx = hea[u]; hea[u] = tot; 
}
struct node1 {
	int v,d; 
	bool operator < (const node1 & rhs)const {
		return d < rhs.d; 
	}
};
void Dijkstra(){
	for (int i = 1; i <= n; i++)	dis[i] = inf; 
	dis[ 1 ] = 0; 
	mset(book,0);
	priority_queue<node1> q; 
	node1 a; 
	a.v = 1; a.d = 0; 
	q.push(a);
	while ( !q.empty() ){
		node1 now = q.top() ;
		q.pop();
		if ( now.d != dis[now.v] )			continue; 
		for (int i = hea[ now.v ] ; i ; i = e[i].nx ){
			if ( dis[ e[i].v ] > dis[ e[i].u  ] + e[i].w ){
				dis[ e[i].v ] = dis[ e[i].u  ] + e[i].w;
				node1 b; 
				b.v = e[i].v; b.d = dis[ e[i].v ] ;
				q.push( b );
			}
		}
	}
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
	 
	n = read();
	char s[20];
	for(int i=2;i<=n;i++)
	for(int j=1;j<i;j++)
	{
		scanf("%s",s);
		if(s[0]!='x'){
			add_ead(i,j,atoi(s));
			add_ead(j,i,atoi(s));
		}
	}	
	Dijkstra();
	int ans = -inf ; 
	for (int i = 2; i <= n; i++){
		if ( ans < dis[i] )
			ans = dis[i] ;
	}
	cout<<ans;
	return 0;
}
 类似资料: