Professional Documents
Culture Documents
#include <stdio.h>
#include <iostream>
#include <map>
#define oo 0xffff
using namespace std;
int main() {
int n, m, cases = 0, i, j, k;
char a[50], b[50];
while(scanf("%d %d", &n, &m) == 2) {
if(n == 0 && m == 0)
break;
int g[51][51], x, y;
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++)
g[i][j] = oo;
g[i][i] = 0;
}
map<string, int> R;
map<string, int>::iterator it;
int size = 0;
while(m--) {
scanf("%s %s", a, b);
it = R.find(a);
if(it == R.end())
R[a] = ++size, x = size;
else x = it->second;
it = R.find(b);
if(it == R.end())
R[b] = ++size, y = size;
else y = it->second;
g[x][y] = g[y][x] = 1;
}
for(k = 1; k <= n; k++) {
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
if(g[i][j] > g[i][k] + g[k][j])
g[i][j] = g[i][k] + g[k][j];
}
}
}
int mx = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
if(g[i][j] > mx)
mx = g[i][j];
printf("Network %d: ", ++cases);
if(mx == oo)
puts("DISCONNECTED");
else
printf("%d\n", mx);
puts("");
}
return 0;
}
c++ code:
//ak92thelooser
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std ;
int main(){
int p , r , i , j ;
string a , b ;
bool f ; int ans ,kase = 0;
dic.clear () ;
for ( i = 0 ; i < p ; i++)
for ( j = 0 ; j < p ; j++)
if ( i != j)
network [i][j] = 1000;
else
network[i][j] = 0;
j=0; f = true ;
//Flyod Warshall
for ( int k = 0 ; k < p ; k++)
for ( i = 0 ; i < p ; i++)
for ( j = 0 ; j < p ; j++)
network [i][j] = min ( network [i][j] , network [i][k] + network [k]
[j]);
ans = 0 ;
for ( i = 0 ; i < p ; i++){
for ( j = 0 ; j < p ; j++)
ans = max ( ans , network[i][j]) ;
printf("\n");
}
return 0 ;
}
Language: C++
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ii = pair<int,int>;
using ll = long long;
using llu = unsigned long long;
const int INF = numeric_limits<int>::max() / 2;
const int MAX = 50;
int main() {
int tc = 0;
int V, E;
int m[MAX][MAX];
while (cin >> V >> E, V) {
tc++;
name_index.clear();
fill(&m[0][0], &m[MAX][0], INF);
for (int i = 0; i < V; i++) m[i][i] = 0;
for (int i = 0; i < E; i++) {
string a, b;
cin >> a >> b;
int u = get_index(a);
int v = get_index(b);
m[u][v] = m[v][u] = 1;
}
int d = 0;
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++) d = max(d, m[i][j]);
if (d == INF) {
printf("Network %d: DISCONNECTED\n\n", tc);
} else {
printf("Network %d: %d\n\n", tc, d);
}
}
}
En informática, el algoritmo de Floyd-Warshall, descrito en 1959 por Bernard Roy, es
un algoritmo de análisis sobre grafos para encontrar el camino mínimo en grafos dirigidos
ponderados. El algoritmo encuentra el camino entre todos los pares de vértices en una
única ejecución. El algoritmo de Floyd-Warshall es un ejemplo de programación dinámica.