可以用最短路算法的原理MST性质去思考这道题,于是就有了Dijkstra算法的变形。注意当安全值为0时表示不连通。
精简的CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 1010;
const int INF = -110;
double w[SIZE][SIZE];
double d[SIZE];
int v[SIZE];
int n;
void Dijkstra(int s, int e) //start, end;
{
int i, j;
memset(v, 0, sizeof(v));
for(i = 1; i <= n; i++) d[i] = (i == s)? 1 : INF; //建图是注意是1还是0,这很重要
for(i = 1; i < n; i++)
{
int x;
double m = INF;
for(int y = 1; y <= n; y++) if(!v[y] && d[y] > m) m = d[x=y]; //d[y] > m;
if(x == e || m == INF) break;
v[x] = 1;
for(int y = 1; y <= n; y++) if(!v[y]) d[y] >?= d[x]*w[x][y]; //d[x]*w[x][y];
}
if(d[e]) printf("%.3lf\n", d[e]);
else printf("What a pity!\n");
} //Dijkstra
int main()
{
int i, j;
while(~scanf("%d", &n))
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%lf", &w[i][j]);
}
}
int T;
for(scanf("%d", &T); T > 0; T--)
{
int s, e;
scanf("%d%d", &s, &e);
Dijkstra(s, e);
}
}
return 0;
}
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 1010;
const int INF = -110;
double w[SIZE][SIZE];
double d[SIZE];
int v[SIZE];
int n;
void Dijkstra(int s, int e) //start, end;
{
int i, j;
memset(v, 0, sizeof(v));
for(i = 1; i <= n; i++) d[i] = (i == s)? 1 : INF; //建图是注意是1还是0,这很重要
for(i = 1; i < n; i++)
{
int x;
double m = INF;
for(int y = 1; y <= n; y++) if(!v[y] && d[y] > m) m = d[x=y]; //d[y] > m;
if(x == e || m == INF) break;
v[x] = 1;
for(int y = 1; y <= n; y++) if(!v[y]) d[y] >?= d[x]*w[x][y]; //d[x]*w[x][y];
}
if(d[e]) printf("%.3lf\n", d[e]);
else printf("What a pity!\n");
} //Dijkstra
int main()
{
int i, j;
while(~scanf("%d", &n))
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%lf", &w[i][j]);
}
}
int T;
for(scanf("%d", &T); T > 0; T--)
{
int s, e;
scanf("%d%d", &s, &e);
Dijkstra(s, e);
}
}
return 0;
}
麻烦点的CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 1010;
const int INF = -110;
double graph[SIZE][SIZE];
double dis[SIZE];
int vis[SIZE];
int n;
void Dijkstra(int u, int v)
{
int i, j;
double max;
int k;
memset(vis, 0, sizeof(vis));
for(i = 1; i <= n; i++) dis[i] = graph[u][i];
dis[u] = 1;
vis[u] = 1;
for(i = 1; i < n; i++)
{
max = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] > max)
{
k = j;
max = dis[j];
}
}
if(k == v || max == INF) break;
vis[k] = 1;
for(j = 1; j <= n; j++)
{
if(!vis[j] && (max*graph[k][j] > dis[j]))
{
dis[j] = max * graph[k][j];
}
}
}
if(dis[v]) printf("%.3lf\n", dis[v]);
else printf("What a pity!\n");
}
int main()
{
int i, j;
while(~scanf("%d", &n))
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%lf", &graph[i][j]);
}
}
int T;
scanf("%d", &T);
while(T--)
{
int u, v;
scanf("%d%d", &u, &v);
Dijkstra(u, v);
}
}
return 0;
}
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 1010;
const int INF = -110;
double graph[SIZE][SIZE];
double dis[SIZE];
int vis[SIZE];
int n;
void Dijkstra(int u, int v)
{
int i, j;
double max;
int k;
memset(vis, 0, sizeof(vis));
for(i = 1; i <= n; i++) dis[i] = graph[u][i];
dis[u] = 1;
vis[u] = 1;
for(i = 1; i < n; i++)
{
max = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] > max)
{
k = j;
max = dis[j];
}
}
if(k == v || max == INF) break;
vis[k] = 1;
for(j = 1; j <= n; j++)
{
if(!vis[j] && (max*graph[k][j] > dis[j]))
{
dis[j] = max * graph[k][j];
}
}
}
if(dis[v]) printf("%.3lf\n", dis[v]);
else printf("What a pity!\n");
}
int main()
{
int i, j;
while(~scanf("%d", &n))
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%lf", &graph[i][j]);
}
}
int T;
scanf("%d", &T);
while(T--)
{
int u, v;
scanf("%d%d", &u, &v);
Dijkstra(u, v);
}
}
return 0;
}