Dijkstra poj1502

//poj 1502 Dijkstra
//
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

const int MAXN=101;
const int INF=100000000;
int map[MAXN][MAXN];
int N;

// 各数组都从下标1开始
int dist[MAXN];     // 表示当前点到源点的最短路径长度
int pre[MAXN];     // 记录当前点的前一个结点
//int c[MAXN][MAXN];   // 记录图的两点间路径长度
//int n, line;         // 图的结点数和路径数

// n -- n nodes
// v -- the source node
// dist[] -- the distance from the ith node to the source node
// pre[] -- the preious node of the ith node
// c[][] -- every two nodes' distance
void Dijkstra(int n, int v, int *dist, int *pre, int c[MAXN][MAXN])
{
	bool s[MAXN];    // 判断是否已存入该点到S集合中
	for(int i=1; i<=n; ++i)
	{
		dist[i] = c[v][i];
		s[i] = 0;     // 初始都未用过该点

		if(dist[i] == INF)
			pre[i] = 0;
		else
			pre[i] = v;
	}
	dist[v] = 0;
	s[v] = 1;

	// 依次将未放入S集合的结点中,取dist[]最小值的结点,放入结合S中
	// 一旦S包含了所有V中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度
    // 注意是从第二个节点开始,第一个为源点
	for(int i=2; i<=n; ++i)
	{
		int tmp = INF;
		int u = v;
		// 找出当前未使用的点j的dist[j]最小值
		for(int j=1; j<=n; ++j)
			if((!s[j]) && dist[j]<tmp)
			{
				u = j;              // u保存当前邻接点中距离最小的点的号码
				tmp = dist[j];
			}
		s[u] = 1;    // 表示u点已存入S集合中

		// 更新dist
		for(int j=1; j<=n; ++j)
			if((!s[j]) && c[u][j]<INF)
			{
				int newdist = dist[u] + c[u][j];
				if(newdist < dist[j])
				{
					dist[j] = newdist;
					pre[j] = u;
				}
			}
	}
	//cout<<"-----"<<endl;
	//for(int i=1;i<=N;i++)
	//	cout<<dist[i]<<" ";
}

void input()
{
	char st[100];
	for (int i = 1; i < N; i++)
		for (int j = 0; j < i; j++)
		{
			scanf("%s", st);
			if (st[0] == 'x')
				map[i][j] = map[j][i] = INF;
			else
				map[i][j] = map[j][i] = atoi(st);
		}
}

int main()
{
    //cout << "Hello world!" << endl;
    //freopen("input.txt","r",stdin);
    //memset(pre,0,sizeof(pre));
    //int T;
    cin>>N;
	/*
    for(int i=1;i<N;i++)
    {
        for(int j=1;j<=i;j++)
        {
            //cin>>map[i][j];
            /*
            scanf("%d",&T);
            if(T==)map[i][j]=INF;
            else map[i][j]=T;
            
            char str[10];
            scanf("%s",&str);
            if(str[0]=='x')
            {
                //map[i][j]=INF;
                map[j][i]=map[i][j]=INF;

            }
            else
                map[i][j]=map[j][i]=atoi(str);
				//map[i][j]=atoi(str);

        }
    }
	*/
	input();
    //Output map[][]
	/*
    for(int i=1;i<N;i++)
    {
        for(int j=1;j<N;j++)
        {
            cout<<map[i][j]<<" ";
        }
        cout<<endl;
    }
	*/
    //init
    for(int i=1; i<=N; i++)
		dist[i] = INF;

    Dijkstra(N-1,0,dist,pre,map);

    //cout<<dist[N]<<endl;
	//cout<<"testing"<<endl;
    //int max=-1;
    //for(int i=1;i<=N;++i)
    //{
        //sum+=dist[i];
        //cout<<dist[i]<<" ";
	//	if(dist[i]>max)
	//		max=dist[i];
    //}
    //cout<<max<<endl;
	int ans = 0;
	for (int i = 0; i < N; i++)
		ans = max(ans, dist[i]);
	cout<<ans<<endl;
    //fclose(stdin);
    return 0;
}

注意 有向图中结点中,自己到自己的结点距离为0。即对角线中的元素为0,特别注意在构造邻接矩阵时。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值