Floyd求最短路
例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const int N = 5000005;
const ll ds = 1e15+7;
//const double p1 = 3.141592653589793238462643383;
using namespace std;
int n,m;
int ans[505][505];
void solve()
{
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
if(i == j) ans[i][j] = 0;
else ans[i][j] = 1e9;
}
}
for(int i = 1; i <= m; i++){
int a,b,x;
cin >> a >> b >> x;
if(ans[a][b] > x) ans[a][b] = x;
if(ans[b][a] > x) ans[b][a] = x;
}
int s,t;
cin >> s >> t;
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
if(ans[i][k] != 1e9)
for(int j = 0; j < n; j++){
if(ans[i][j] > ans[i][k]+ans[k][j])
ans[i][j] = ans[i][k]+ans[k][j];
}
}
}
if(ans[s][t] == 1e9) cout << "-1\n";
else cout << ans[s][t] << endl;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
solve();
}
return 0;
}
例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1596
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const int N = 5000005;
const ll ds = 1e15+7;
//const double p1 = 3.141592653589793238462643383;
using namespace std;
int n;
double ans[1005][1005];
void solve()
{
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%lf",&ans[i][j]);
}
}
int s,t;
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
if(ans[i][k] != 0)
for(int j = 0; j < n; j++){
if(ans[i][j] < ans[i][k]*ans[k][j])
ans[i][j] = ans[i][k]*ans[k][j];
}
}
}
int q;
scanf("%d",&q);
while(q--){
scanf("%d%d",&s,&t);
if(ans[s-1][t-1] == 0) printf("What a pity!\n");
else printf("%.3f\n",ans[s-1][t-1]);
}
}
int main()
{
while(~scanf("%d",&n)){
solve();
}
return 0;
}