• 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;
    }


  • 相关阅读:
    codeforce 272B Dima and Sequence
    Codeforce 270D Greenhouse Effect
    codeforce 270C Magical Boxes
    codeforce 270B Multithreading
    图论--Dijkstra算法总结
    图论--(技巧)超级源点与超级汇点
    图论--Floyd总结
    ZOJ 3932 Handshakes
    ZOJ 3932 Deque and Balls
    ZOJ 3927 Programming Ability Test
  • 原文地址:https://www.cnblogs.com/lj030/p/3002302.html
Copyright © 2020-2023  润新知