hdu 1874 畅通工程续 单源最短路径

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874

题意:畅通工程续,单源最短路径,dijkstra

#include <iostream>
#include <math.h>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <assert.h>
#include <list>
using namespace std;

/************************************************************************/
/* disco算法实现                                                     */
/************************************************************************/
int dist[205];
int tab[205][205];
bool visited[205];
int shortpath,s,d;

void disco(int n)
{
	for(int i=0; i<n; i++)
		dist[i] = tab[s][i];

	visited[s] = true;
	dist[s] = 0;
	for(int i=1; i<n; i++)
	{
		int min = 99999999,k = s;
		for(int j=0; j<n; j++)
		{
			if(min>dist[j] && !visited[j])
				min = dist[j],k = j;
		}// for
		
		visited[k] = true;

		for(int j=0; j<n; j++)
		{
			if(!visited[j] /*&& tab[k][j]<99999999*/ && dist[k]+tab[k][j]<dist[j])
				dist[j] =dist[k]+tab[k][j];
		}// for
	}// for

	if(dist[d]==99999999)
		cout << -1 << endl;
	else
		cout << dist[d] << endl;
}

void hdu1874()
{
	//freopen("sample.txt","r",stdin);
	int n,m;
	while(cin>>n>>m)
	{
		shortpath = 0;
		::memset(visited,0,sizeof(visited));
		for(int i=0; i<205; i++)
		{
			dist[i] = 99999999;
			for(int j=0; j<205; j++)
				tab[i][j] = 99999999;
		}

		int src,dest,cost;
		for(int i=0; i<m; i++)
		{
			cin >> src >> dest >> cost;
			//	注意,数据的处理两个村之间可能存在两条直接路径,所以要取最小的 
			if(tab[src][dest] > cost)
				tab[src][dest] = tab[dest][src] = cost;

		}// for;
		cin >> s >> d;
		disco(n);
	}// while

}

int main()
{
	hdu1874();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值