The main idea is that there is a message to be sent from the first processor to the remaining individual processors, for the longest time required.
Compare the basic shortest-circuit problem, SPFA once over. However, this problem data gives a strange way, it gives a lower triangular matrix, and does not give the main diagonal data. The data on the diagonal is recorded as 0 by test instructions, and then the matrix is symmetric about the main diagonal. There is a character x in the data, which means that it cannot be communicated. In this case, the method of scanf ("%s", ch) +atoi (CH) can be solved.
Code:
#include <cstdio> #include <cstdlib> #include <cstring> #include <queue> #include <iostream&
Gt
using namespace Std;
const int inf=0x3f3f3f3f;
int n;
int mm[105][105]={0};
int dis[105]={0};
BOOL Had[105]={0};
Queue <int> q;
void Spfa () {memset (dis,0x3f,sizeof (dis));
Memset (Had,0,sizeof (had));
Dis[0]=0;q.push (0);
while (!q.empty ()) {int T=q.front (); Q.pop ();
had[t]=0; for (int i=0;i<n;i++) {if (Mm[t][i]!=inf && dis[i]>dis[t]+mm[t][i]) {dis[i]=dis[t]
+mm[t][i];
if (!had[i]) {had[i]=1;
Q.push (i);
}}}}} int main () {cin>>n;
for (int i=1;i<n;i++) {for (int j=0;j<i;j++) {char ch[20];
scanf ("%s", ch);
if (ch[0]== ' x ') Mm[i][j]=mm[j][i]=inf;
else Mm[i][j]=mm[j][i]=atoi (CH);
}} SPFA (); int ans= 0;
for (int i=0;i<n;i++) {if (Dis[i]>ans) ans=dis[i];
} cout<<ans<<endl;
return 0; }