[kuangbin带你飞] 专题四 最短路练习 练习

1、Til the Cows Come Home

 裸的最短路,wa一路发现自己T,N读入反了,据说还有重边,不过是邻接矩阵需要考虑的问题,前向星和邻接表可以忽略。

#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
typedef pair Pi;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e6 + 10;
struct _edge{
    int to,nex,w;
}edge[maxn<<1];
int head[maxn],tot;
int dis[maxn];
int n,m,u,v,w;
void add(int u,int v,int w){
    edge[tot] = (_edge){v,head[u],w};
    head[u] = tot++;
}
void dij(int s){
    memset(dis,0x3f,sizeof(dis)); dis[s] = 0;
    priority_queue< Pi,vector,greater > pq; 
    pq.push(make_pair(0,s));   
    while(!pq.empty()){
        Pi now = pq.top(); pq.pop();
        int u = now.second;
        if (dis[u] < now.first) continue;
        for(int i=head[u]; ~i; i=edge[i].nex){
            int v = edge[i].to;
            if (dis[v] > dis[u] + edge[i].w){
                dis[v] = dis[u] + edge[i].w;
                pq.push(make_pair(dis[v],v));
            }
        }
    }
}
int main(){
    scanf("%d%d",&m,&n);
    memset(head,-1,sizeof(head));
    for(int i=0; i

 

2、Frogger

 弗洛伊德有dp的性质,找一条,每一跳都尽可能小的路径,找这个路径上的跳跃最大的距离

用 dis[i][j] 表示 i 到 j 距离最大的距离

则有  dis[i][j] = min(max(dis[i][k],dis[k][j]), dis[i][j]);

中间的max是路径上跳跃的最大距离,而外层的min代表最大跳跃距离尽可能小的路径

#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
int Gcd(int a,int b){if (b == 0) return a; return Gcd(b , a%b);}
int Lcm(int a, int b){ return a/Gcd(a,b)*b;}
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e3 + 10;
double dis[maxn][maxn];
double px[maxn],py[maxn];
int n;
int main(){
// /    freopen("/Users/chutong/ACM/data.in","r",stdin);
    int cas = 0;
    while(scanf("%d",&n) == 1 && n){
        cas++;
        for(int i=1; i<=n; i++){
            scanf("%lf%lf",&px[i],&py[i]);
            for(int j=1;j<=i-1; j++){
                dis[j][i] = dis[i][j] = sqrt((px[i]-px[j])*(px[i]-px[j]) + (py[i]-py[j])*(py[i]-py[j]));
            }
        }
        for(int k=1; k<=n; k++){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    if (i != k && j != k)
                        dis[i][j] = min(max(dis[i][k],dis[k][j]),dis[i][j]);
                }
            }
        }
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",cas,dis[1][2]);
    }
    return 0;
}

 

你可能感兴趣的:(Kuangbin最短路)