设为首页 加入收藏

TOP

CSU-1307-CityTour+Dij+Kruskal
2014-11-23 21:46:33 来源: 作者: 【 】 浏览:9
Tags:CSU-1307-CityTour Dij Kruskal
/*
最短路+最小生成树
题意:给定一张图,起点,终点。求起点到终点的一条路(这条路经过的最长的一段要最短!)
	枚举这条“最长的路”,可二分,也可直接计算出。
*/
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 2005;
const int maxm = 50005;
const int inf = 99999999;

int mat[ maxn ][ maxn ];
int fa[ maxn ];
bool vis[ maxn ];
int dis[ maxn ];
struct Node{
	int x,y,val;
}edge[ maxm<<1 ];

int cmp( Node a,Node b ){
	return a.valy ) fa[y] = x;
			else fa[x] = y;
			if( find(A)==find(B) ){
				MaxEdge = edge[i].val;
				return MaxEdge;
			}
			MaxEdge = max( MaxEdge,edge[i].val );
		}
	}
	return MaxEdge;
}

int Dij( int n,int MaxEdge,int A,int B ){
	for( int i=1;i<=n;i++ ){
		vis[i] = false;
		dis[i] = inf;
	}
	dis[ A ] = 0;
	for( int i=1;i<=n;i++ ){
		int M = inf;
		int id = -1;
		for( int j=1;j<=n;j++ ){
			if( !vis[j] && M>dis[j] ){
				M = dis[j];
				id = j;
			}
		}
		if( id==-1 ) break;
		vis[ id ] = true;
		for( int j=1;j<=n;j++ ){
			if( !vis[j] && dis[j]>dis[id]+mat[id][j] && mat[id][j]<=MaxEdge ){
				dis[j] = dis[id]+mat[id][j];
			}
		}
	}
	return dis[B];
}

int main(){
	//freopen("in.txt","r",stdin);
	int n,m,A,B;
	while( scanf("%d%d%d%d",&n,&m,&A,&B)!=EOF ){
		init( n );
		for( int i=0;iedge[i].val ){
				mat[edge[i].x][edge[i].y] = mat[edge[i].y][edge[i].x] = edge[i].val;
			}
		}
		int MaxEdge = 0;
		MaxEdge = Kruskal( n,m,A,B );
		//printf("MaxEdge = %d\n",MaxEdge);
		int Sum = Dij( n,MaxEdge,A,B );
		printf("%d\n",Sum);
	}
}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇[LeetCode]Populating Next Right.. 下一篇poj 3271 Lilypad Pond bfs

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·深入理解 Java 集合 (2025-12-27 07:22:48)
·Java集合框架全面解 (2025-12-27 07:22:45)
·时隔 15 年,巨著《J (2025-12-27 07:22:43)
·定义一个类模板并实 (2025-12-27 06:52:28)
·一文搞懂怎么用C语言 (2025-12-27 06:52:25)