图论专题(二)POJ1125最短路

来源:互联网 发布:it服务工程师培训 编辑:程序博客网 时间:2024/05/16 06:28
//题目连接: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);    }}


原创粉丝点击