一个Floyd最短路,一个EdmondsKarp最大流
Run Time: 0.128s
#define UVa "11-1.821.cpp" //Page Hopping
char fileIn[30] = UVa, fileOut[30] = UVa;
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
//Global Variables. Reset upon Each Case!
const int maxn = 100 + 5, INF = 1<<30;
int d[maxn][maxn];
int cnt;
int ID[maxn];
/
int getID(int a) {
if(ID[a] == -1) return ID[a] = ++cnt;
return ID[a];
}
bool read() {
cnt = 0;
memset(ID, -1, sizeof(ID));
for(int i = 0; i < maxn; i ++) for(int j = 0; j < maxn; j ++) d[i][j] = INF;
int a, b;
scanf("%d%d", &a, &b);
if(!a) return false;
while(a){
a = getID(a), b = getID(b);
d[a][b] = 1;
scanf("%d%d", &a, &b);
}
return true;
}
void floyd() {
for(int k = 1; k <= cnt; k ++)
for(int i = 1; i <= cnt; i ++)
for(int j = 1; j <= cnt; j ++)
if(d[i][k] != INF && d[k][j] != INF)
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
}
int main() {
int kase = 0;
while(read()) {
floyd();
int a, b;
long long sum = 0;
for(int i = 1; i <= cnt; i ++)
for(int j = 1; j <= cnt; j ++)
if(i!=j)
sum += d[i][j];
printf("Case %d: average length between pages = %.3f clicks\n", ++kase, (float)sum/(float)(cnt*(cnt-1)));
}
return 0;
}
Run Time: 0.022s
#define UVa "11-3.820.cpp" //Internet Bandwidth
char fileIn[30] = UVa, fileOut[30] = UVa;
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
//Global Variables. Reset upon Each Case!
const int maxn = 100 + 5, INF = 1<<30;
int n, s, t, c;
/
struct Edge {
int from, to, cap, flow;
Edge(int a1, int a2, int a3): from(a1), to(a2), cap(a3), flow(0){};
};
struct EdmondsKarp {
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn], p[maxn];
void init() {
for(int i = 0; i < maxn; i ++) G[i].clear();
edges.clear();
}
void addEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap));
edges.push_back(Edge(to, from, cap));
G[from].push_back(edges.size()-2);
G[to].push_back(edges.size()-1);
}
int maxFlow(int src, int targ) {
int flow = 0;
for(;;) {
memset(a, 0, sizeof(a));
queue<int> q;
q.push(src);
a[src] = INF;
while(!q.empty()) {
int x = q.front(); q.pop();
for(int i = 0; i < G[x].size(); i ++) {
Edge& e = edges[G[x][i]];
int y = e.to;
if(a[y] == 0 && e.cap > e.flow) {
a[y] = min(a[x], e.cap - e.flow);
p[y] = G[x][i];
q.push(y);
}
}
if(a[targ]) break;
}
if(!a[targ]) break;
for(int u = targ; u != src; u = edges[p[u]].from) {
edges[p[u]].flow += a[targ];
edges[p[u]^1].flow += a[targ];
}
flow += a[targ];
}
return flow;
}
};
int main() {
// #define DEBUG 1
// #ifdef DEBUG
// strncpy(&fileIn[strlen(fileIn) - 4], ".in\0", 5); strncpy(&fileOut[strlen(fileOut) - 4], ".out\0", 5);
// freopen(fileIn,"r",stdin);
freopen(fileOut,"w",stdout);
// #endif
EdmondsKarp EK;
int kase = 0;
while(scanf("%d", &n) && n) {
EK.init();
scanf("%d%d%d", &s, &t, &c);
int from, to, cap;
for(int i = 0; i < c; i ++) {
scanf("%d%d%d", &from, &to, &cap);
EK.addEdge(from, to, cap);
}
printf("Network %d\nThe bandwidth is %d.\n\n", ++kase, EK.maxFlow(s, t));
}
return 0;
}