zoukankan      html  css  js  c++  java
  • zoj1082Stockbroker Grapevine

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int n,g[101][101],d[101];
    const int INF=10000000;
    int broken;
    int spfa(int x)
    {
         queue <int > q;
    	 q.push(x);
         bool inq[101];
    	 memset(inq,0,sizeof(inq));
    	 for(int i=1;i<=n;i++)d[i]=(i==x?0:INF);
    	 while(!q.empty())
    	 {
    		 int t=q.front();
    		 q.pop();
    		 inq[t]=0;
    		 for(int i=1;i<=n;i++)
    		 {
    			 if(g[t][i]&&d[i]>d[t]+g[t][i])
    			 {
                       d[i]=d[t]+g[t][i];
    				   if(!inq[i])
    				   {
    					   inq[i]=1;
    					   q.push(i);
    				   }
    			 }
    		 }
    	 }
    	 broken=0;
    	 for(int i=1;i<=n;i++)
    		 if(d[i]==INF)broken=1;
    	 int mint=-INF;
    	 for(int i=1;i<=n;i++){mint=max(d[i],mint);}
    	 return mint;
    }
    
    int main()
    {
        while(cin>>n&&n)
    	{
    		int m,k,w;
    		memset(g,0,sizeof(g));
    		for(int i=1;i<=n;i++)
    		{
                cin>>m;
    			for(int j=1;j<=m;j++)
    			{
    				cin>>k>>w;
    				g[i][k]=w;
    			}
    		}
    		int ANS=-1,tmp=INF,haha;
    	    for(int i=1;i<=n;i++)
    		{
    			haha=spfa(i);
    			if(tmp>haha&&!broken){tmp=haha;ANS=i;}
    		}
    		if(ANS==-1)cout<<"disjoint"<<endl;
    		else cout<<ANS<<" "<<tmp<<endl;
    	}
    }
    
  • 相关阅读:
    c基础
    一维数组,字符数组
    循环结构
    分支结构
    结构体
    Python简介和入门
    Python基础(一)
    Markdown 基础学习
    PyCharm 专业版安装
    Python基础(二)
  • 原文地址:https://www.cnblogs.com/sook/p/2024065.html
Copyright © 2011-2022 走看看