图论专题(二)POJ1125最短路

17 篇文章 0 订阅
/ /题目连接:http://poj.org/problem?id=1125

//本题是基本的最短路问题

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
struct node
{
    int dis , wz;
    bool operator < (node a)const
    {
        if(dis > a.dis)return 1;
        return 0;
    }
}test[111];
int n,map[111][111];
vector<int>pass[111];
int bellman(int x)
{
    bool vis[111];
    priority_queue<node> m;
    for(int i = 1 ; i <= n ; i++)
    {
        test[i].dis = 0x3f3f3f;
        test[i].wz = i;
    }
    memset(vis,0,sizeof(vis));
    vis[x] = 1;
    vector<int>::iterator it;
    int t = x;
    test[t].dis = 0;
    for(int i = 1 ; i <= n ; i++)
    {
        for(it = pass[t].begin() ; it < pass[t].end() ; it++)
        {
            if(test[*it].dis > test[t].dis + map[t][*it])
            {
                test[*it].dis = test[t].dis + map[t][*it];
                m.push(test[*it]);
            }
        }
        bool cas = 1;
        while(!m.empty() && cas)
        {
            node a = m.top();
            if(!vis[a.wz])
            {
                cas = 0;
                t = a.wz;
                vis[a.wz] = 1;
            }
            else cas = 1;
            m.pop();
        }
      //  if(m.empty())break;
    }
    int ans = 0;
    for(int i = 1 ; i <= n ; i++)
    {
        if(test[i].dis == 0x3f3f3f)return 0x3f3f3f;
        ans = max(ans,test[i].dis);
    }
    return ans;

}
int main()
{
    while(scanf("%d",&n) && n != 0)
    {
        for(int i = 1 ; i <= n ; i++)
        {
            int pair,x,y;
            pass[i].clear();
            scanf("%d",&pair);
            for(int j = 1 ; j <= pair ; j++)
            {
                scanf("%d%d",&x,&y);
                map[i][x] = y;
                pass[i].push_back(x);
            }
        }
        int res = 0x3f3f3f;
        int ll ;
        for(int i = 1 ; i <= n ; i++)
        {
            int pp = bellman(i) ;
            if(res > pp)
            {
                res = pp;
                ll = i;
            }
        }
        if(res == 0x3f3f3f)printf("disjoint\n");
        else printf("%d %d\n",ll,res);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值