/*floyd*/
#include <iostream>
#define N 201
//sometimes it(Inifite we define) plus an integer,if it is 0x7fffffff,it will overflow and
//becomes an negative integer.And it will be smaller than anyone.
//Don't use memset to initial an array,it works on binary.If the array is supposed to be 1 or 0,
//the memset function will be a good choice.
//I make mistakes when I come to find Max and find Min actually!The exchange of index confuse me.OMG...
//Every single column or row can be treated as a point to the others in floyd algorithm.
//The path is the shortest.
#define Inifite 0x3fffffff
using namespace std;
int edge[N][N];
int numOfBrokers;
int smaller(int a,int b)
{
return a>b?b:a;
}
int findOneMax(int i)
{
int index=1;
for (int j=1;j<=numOfBrokers;++j)
{
if(edge[i][index]<edge[i][j])
index=j;
}
return index;
}
void Initial()
{
for (int i=1;i<=numOfBrokers;i++)
{
for (int j=1;j<=numOfBrokers;j++)
{
if(i==j) edge[i][j]=0;
else edge[i][j]=Inifite;
}
}
}
void floyd()
{
int minLength=Inifite,minIndex=1;
for (int k=1;k<=numOfBrokers;++k)
{
int columnMax;
for (int i=1;i<=numOfBrokers;++i)
{
for (int j=1;j<=numOfBrokers;++j)
{
edge[i][j]=smaller(edge[i][j],edge[i][k]+edge[k][j]);
}
columnMax=findOneMax(i);//the maxIndex of the i-st column
if (edge[i][columnMax]<minLength)
{
minIndex=i;
minLength=edge[i][columnMax];
}
}
}
if (minLength==Inifite)
cout << "disjoint" << endl;
else cout << minIndex << " " << minLength << endl;
}
int main()
{
//freopen("in.txt","r",stdin);
int from,to,costMinutes,j=1;
while (cin >> numOfBrokers &&numOfBrokers)
{
//situation just like this:has many times inputs,remember initial some
//necessary variations at first,especially counters or outputs
int minLength=Inifite,minIndex=1,temp,j=1;//every time j will be set as 1;
Initial();
while (j<=numOfBrokers)
{
int contracts,i=1;
cin >> contracts;
while (i<=contracts)
{
from=j;
cin >> to >> costMinutes;
edge[from][to]=costMinutes;
++i;
}
++j;
}
floyd();
}
}
poj1125之floyd
最新推荐文章于 2020-07-01 00:14:53 发布