zoukankan      html  css  js  c++  java
  • poj 1125

    最短路径问题,我用的是临接表来存储各个边的信息,用优先队列的dijkstra算法
    #include <iostream>
    #include <queue>
    #include <vector>
    using namespace std;
    const int maxn=101;
    const int INF=2<<20;
    struct edge 
    {
    	int po,w;
    	edge * next;
    };
    struct node
    {
    	edge * first;
    }head[maxn];
    typedef pair<int,int>pii;
    priority_queue<pii,vector<pii>,greater<pii> >q;
    int n,per,minx;
    void dijkstra()
    {
    	int low[maxn],i,j,visit[maxn];
    	minx=INF;
    	for(i=1;i<=n;i++)
    	{
    		int max=-1;
    		int tot=0;
    		for(j=1;j<=n;j++)
    		{
    			low[j]=j==i?0:INF;
    			visit[j]=0;
    		}
    		q.push(make_pair(low[i],i));
    		while(!q.empty())
    		{
    			pii u=q.top();
    			q.pop();
    			int x=u.second;
    			if(visit[x]) continue;
    			visit[x]=1;
    			tot++;
    			if(low[x]>max)
    				max=low[x];
    			edge * tem=head[x].first;
    			while(tem!=NULL)
    			{
    				if(low[tem->po]>(low[x]+tem->w))
    				{
    					low[tem->po]=low[x]+tem->w;
    					q.push(make_pair(low[tem->po],tem->po));
    				}
    				tem=tem->next;
    			}
    		}
    		if(tot==n)
    		{
    		if(max<minx)
    		{
    			minx=max;
    			per=i;
    		}
    		}
    	}
    }
    int main()
    {
    	while(cin>>n&&n!=0)
    	{
    		int i,t,e,w;
    		for(i=1;i<=n;i++) head[i].first=NULL;
    		edge * tem;
    		for(i=1;i<=n;i++)
    		{
    			cin>>t;
    			while(t--)
    			{
    				cin>>e>>w;
    				tem=new edge;
    				tem->po=e;
    				tem->w=w;
    				tem->next=head[i].first;
    				head[i].first=tem;
    			}
    		}
    		dijkstra();
    		cout<<per<<" "<<minx<<endl;
    	}
    	return 0;
    }


  • 相关阅读:
    编译环境
    好看的函数图
    R语言学习
    python画图
    C++技巧
    DLL
    进程共享数据
    八数码问题
    document.getElementsByTagName()返回的是数组document.getElementsById()是dom内容
    使用Math.max,Math.min获取数组中的最值
  • 原文地址:https://www.cnblogs.com/lj030/p/3002302.html
Copyright © 2011-2022 走看看