hdu 2962 题解

题目

题意

给出一张图,每条道路有限高,给出车子的起点,终点,最高高度,问在保证高度尽可能高的情况下的最短路,如果不存在输出 $ cannot ?reach ?destination $

跟前面 $ hdu5418 $ 一样的,题目挺基础的,但是在细节方面比较抠。要是最高度尽可能高,我们就可以想去枚举可能的高度去跑 $ spfa $ ,但我们可以发现这样效率太低了,那么我们为什么不二分答案呢?给出最高高度了,最低不就是 $ 0 $ 吗?二分答案再去跑 $ spfa $ 加上限制条件。 这题时限 $ 10s $ 枚举其实一样能过,也跑得挺快的。另外我还敲了敲 $ floyd $ 果不其然,数据范围太大,它挂了。dijkstra它我也敲挂了(答案错误)等改对了再说吧。

比较坑的点:每组答案之间输出换行,因为这个格式错误了好几次。

代码(按时间从小到大排序

dijkstra(堆优化)+二分

$ 187MS $

/*
dijkstra(堆优化)+二分
Judge Status:Accepted
Exe.Time:187MS
Exe.Memory:1752K
Code Len:2640 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}edge[M];
int head[N],ver[M],Next[M],ans,minf,n,m,tot,star,en,sum,addmin;
void add(int x,int y,int z,int hi){
    ver[++tot]=y;edge[tot].data=z;edge[tot].h=hi;Next[tot]=head[x];head[x]=tot;
}
struct nod{
    int data;
    int num;
}minn[M];
int d[N];
bool book[N];
void shiftdownmin(int x){
    int t,flag=0;
    while(x*2<=addmin&&flag==0){
        if(minn[x].data>minn[x*2].data)t=x*2;
        else t=x;
        if(x*2+1<=addmin){
            if(minn[t].data>minn[x*2+1].data)t=x*2+1;
        }
        if(t!=x){
            swap(minn[t],minn[x]);
            x=t;
        }else flag=1;
    }
}
void shiftupmin(int x) {
    int flag=0;
    if(x==1) return;
    while(x!=1&&flag==0){
        if(minn[x].data<minn[x/2].data) swap(minn[x],minn[x/2]);
        else flag=1;
        x=x/2;
    }
}
void dijkstra(int mi){
    memset(book,0,sizeof(book));
    memset(d,0x3f,sizeof(d));addmin=0;
    d[star]=0;
    minn[++addmin].data=0;
    minn[addmin].num=star;
    while(addmin){
        int x=minn[1].num;
        minn[1]=minn[addmin--];
        shiftdownmin(1);
        if(book[x]) continue;
        book[x]=1;
        for(int i=head[x];i;i=Next[i]){
            int y=ver[i],z=edge[i].data;
            if(d[y]>d[x]+z&&edge[i].h>=mi){
                d[y]=d[x]+z;
                minn[++addmin].num=y;
                minn[addmin].data=d[y];
                shiftupmin(addmin);
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(head,0,sizeof(head));tot=0;sum++;
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            add(x,y,z,h);
            add(y,x,z,h);
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minf=0;
        while(l<=r){
            int mid=(l+r)>>1;
            dijkstra(mid);
            if(d[en]!=INF){
                if(mid>ans){
                    ans=mid;
                    minf=d[en];
                } l=mid+1;
            }else r=mid-1;
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minf);
        }else printf("cannot reach destination\n");
    }
    return 0;
}

spfa+二分

$ 218MS $

/*
spfa+二分
Judge Status:Accepted
Exe.Time:218MS
Exe.Memory:1740K
Code Len:1772 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}edge[M];
int head[N],ver[M],Next[M],d[N],v[N],ans,minn,n,m,tot,star,en,sum;
void add(int x,int y,int z,int hi){
    ver[++tot]=y;edge[tot].data=z;edge[tot].h=hi;Next[tot]=head[x];head[x]=tot;
}
queue<int>q;
void spfa(int mi){
    memset(d,0x3f,sizeof(d));
    memset(v,0,sizeof(v));
    d[star]=0;v[star]=1;
    q.push(star);
    while(q.size()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i=head[x];i;i=Next[i]){
            int y=ver[i],z=edge[i].data;
            if((d[y]>d[x]+z)&&edge[i].h>=mi){
                d[y]=d[x]+z;
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(head,0,sizeof(head));tot=0;sum++;
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            add(x,y,z,h);
            add(y,x,z,h);
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minn=0;
        while(l<=r){
            int mid=(l+r)>>1;
            spfa(mid);
            if(d[en]!=INF){
                if(mid>ans){
                    ans=mid;
                    minn=d[en];
                }
                l=mid+1;
            }else r=mid-1;
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minn);
        }else printf("cannot reach destination\n");
    }
    return 0;
}

枚举+spfa

$ 390MS $

/*
spfa+枚举
Judge Status:Accepted
Exe.Time:390MS
Exe.Memory:1768K
Code Len:1668 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}edge[M];
int head[N],ver[M],Next[M],d[N],v[N],ans,minn,n,m,tot,star,en,sum;
void add(int x,int y,int z,int hi){
    ver[++tot]=y;edge[tot].data=z;edge[tot].h=hi;Next[tot]=head[x];head[x]=tot;
}
queue<int>q;
void spfa(int mi){
    memset(d,0x3f,sizeof(d));
    memset(v,0,sizeof(v));
    d[star]=0;v[star]=1;
    q.push(star);
    while(q.size()){
        int x=q.front();q.pop();
        v[x]=0;
        for(int i=head[x];i;i=Next[i]){
            int y=ver[i],z=edge[i].data;
            if((d[y]>d[x]+z)&&edge[i].h>=mi){
                d[y]=d[x]+z;
                if(!v[y]) q.push(y),v[y]=1;
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(head,0,sizeof(head));tot=0;sum++;
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            add(x,y,z,h);
            add(y,x,z,h);
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minn=0;
        for(int i=r;i;--i){
            spfa(i);
            if(d[en]!=INF){
                ans=i;
                minn=d[en];
                break;
            }
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minn);
        }else printf("cannot reach destination\n");
    }
    return 0;
}

dijkstra(堆优化)+枚举

$ 405MS $

/*
dijkstra(堆优化)+枚举
Judge Status:Accepted
Exe.Time:405MS
Exe.Memory:1752K
Code Len:2558 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}edge[M];
int head[N],ver[M],Next[M],ans,minf,n,m,tot,star,en,sum,addmin;
void add(int x,int y,int z,int hi){
    ver[++tot]=y;edge[tot].data=z;edge[tot].h=hi;Next[tot]=head[x];head[x]=tot;
}
struct nod{
    int data;
    int num;
}minn[M];
int d[N];
bool book[N];
void shiftdownmin(int x){
    int t,flag=0;
    while(x*2<=addmin&&flag==0){
        if(minn[x].data>minn[x*2].data)t=x*2;
        else t=x;
        if(x*2+1<=addmin){
            if(minn[t].data>minn[x*2+1].data)t=x*2+1;
        }
        if(t!=x){
            swap(minn[t],minn[x]);
            x=t;
        }else flag=1;
    }
}
void shiftupmin(int x) {
    int flag=0;
    if(x==1) return;
    while(x!=1&&flag==0){
        if(minn[x].data<minn[x/2].data) swap(minn[x],minn[x/2]);
        else flag=1;
        x=x/2;
    }
}
void dijkstra(int mi){
    memset(book,0,sizeof(book));
    memset(d,0x3f,sizeof(d));addmin=0;
    d[star]=0;
    minn[++addmin].data=0;
    minn[addmin].num=star;
    while(addmin){
        int x=minn[1].num;
        minn[1]=minn[addmin--];
        shiftdownmin(1);
        if(book[x]) continue;
        book[x]=1;
        for(int i=head[x];i;i=Next[i]){
            int y=ver[i],z=edge[i].data;
            if(d[y]>d[x]+z&&edge[i].h>=mi){
                d[y]=d[x]+z;
                minn[++addmin].num=y;
                minn[addmin].data=d[y];
                shiftupmin(addmin);
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(head,0,sizeof(head));tot=0;sum++;
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            add(x,y,z,h);
            add(y,x,z,h);
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minf=0;
        for(int i=r;i;--i){
            dijkstra(i);
            if(d[en]!=INF){
                ans=i;
                minf=d[en];
                break;
            }
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minf);
        }else printf("cannot reach destination\n");
    }
    return 0;
}

dijkstra+二分

$ 1154MS $

/*
dijkstra+二分
Judge Status:Accepted
Exe.Time:1154MS
Exe.Memory:9376K
Code Len:2376 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}dis[N][N];
int ans,minn,n,m,tot,star,en,sum;
node d[N];
bool book[N];
void dijkstra(int x){
    memset(book,0,sizeof(book));
    memset(d,0x3f,sizeof(d));
    for(int i=1;i<=n;++i){
        if(dis[star][i].h>=x) d[i].data=dis[star][i].data;
        d[i].h=dis[star][i].h;
    }
    d[star].data=0;book[star]=1;
    for(int i=1;i<n;++i){
        int minx=INF;
        int op=-1;
        for(int j=1;j<=n;++j){
            if(d[j].data<minx&&book[j]!=1&&d[j].h>=x){
                minx=d[j].data;
                op=j;
            }
        }
        if(op!=-1){
            book[op]=1;
            for(int j=1;j<=n;++j){
                if(d[j].data>d[op].data+dis[op][j].data&&(!book[j])&&(dis[op][j].h>=x)&&(d[op].h>=x)){
                    d[j].data=d[op].data+dis[op][j].data;
                    d[j].h=min(d[op].h,dis[op][j].h);
                }
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(dis,0,sizeof(dis));sum++;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if(i==j) dis[i][j].data=0;
                else dis[i][j].data=INF;
            }
        }
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            if(h>dis[x][y].h){
                dis[x][y].data=z;
                dis[x][y].h=h;
            }
            dis[y][x].data=dis[x][y].data;
            dis[y][x].h=dis[x][y].h;
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minn=0;
        while(l<=r){
            int mid=(l+r)>>1;
            dijkstra(mid);
            if(d[en].data!=INF){
                if(mid>ans){
                    ans=mid;
                    minn=d[en].data;
                } l=mid+1;
            }else r=mid-1;
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minn);
        }else printf("cannot reach destination\n");
    }
    return 0;
}

以下是非AC代码

/*
dijkstra+枚举
Judge Status:Time Limit Exceeded
Exe.Time:10000MS
Exe.Memory:9332K
Code Len:2287B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}dis[N][N];
int ans,minn,n,m,tot,star,en,sum;
node d[N];
bool book[N];
void dijkstra(int x){
    memset(book,0,sizeof(book));
    memset(d,0x3f,sizeof(d));
    for(int i=1;i<=n;++i){
        if(dis[star][i].h>=x) d[i].data=dis[star][i].data;
        d[i].h=dis[star][i].h;
    }
    d[star].data=0;book[star]=1;
    for(int i=1;i<n;++i){
        int minx=INF;
        int op=-1;
        for(int j=1;j<=n;++j){
            if(d[j].data<minx&&book[j]!=1&&d[j].h>=x){
                minx=d[j].data;
                op=j;
            }
        }
        if(op!=-1){
            book[op]=1;
            for(int j=1;j<=n;++j){
                if(d[j].data>d[op].data+dis[op][j].data&&(!book[j])&&(dis[op][j].h>=x)&&(d[op].h>=x)){
                    d[j].data=d[op].data+dis[op][j].data;
                    d[j].h=min(d[op].h,dis[op][j].h);
                }
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(dis,0,sizeof(dis));sum++;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if(i==j) dis[i][j].data=0;
                else dis[i][j].data=INF;
            }
        }
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            if(h>dis[x][y].h){
                dis[x][y].data=z;
                dis[x][y].h=h;
            }
            dis[y][x].data=dis[x][y].data;
            dis[y][x].h=dis[x][y].h;
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minn=0;
        for(int i=r;i;--i){
            dijkstra(i);
            if(d[en].data!=INF){
                ans=i;
                minn=d[en].data;
                break;
            }
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minn);
        }else printf("cannot reach destination\n");
    }
    return 0;
}
/*
floyd+枚举
Judge Status:Time Limit Exceeded
Exe.Time:10000MS
Exe.Memory:9740K
Code Len:1819 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}dis[N][N];
int ans,minn,n,m,tot,star,en,sum;
node d[N][N];
queue<int>q;
void floyd(int x){
    memset(d,0x3f,sizeof(d));
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(dis[i][j].h>=x) d[i][j].data=dis[i][j].data;
            d[i][j].h=dis[i][j].h;
        }
    }
     for(int k=1;k<=n;++k){
         for(int i=1;i<=n;++i){
             for(int j=1;j<=n;++j){
                 if((d[i][j].data>d[i][k].data+d[k][j].data)&&(d[i][k].h>=x)&&(d[k][j].h>=x)){
                    d[i][j].data=d[i][k].data+d[k][j].data;
                    d[i][j].h=min(d[i][k].h,d[k][j].h);
                 }
             }
         }
     }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(dis,0,sizeof(dis));sum++;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if(i==j) dis[i][j].data=0;
                else dis[i][j].data=INF;
            }
        }
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            if(h>dis[x][y].h){
                dis[x][y].data=z;
                dis[x][y].h=h;
            }
            dis[y][x].data=dis[x][y].data;
            dis[y][x].h=dis[x][y].h;
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minn=0;
        for(int i=r;i;--i){
            floyd(i);
            if(d[star][en].data!=INF){
                ans=i;
                minn=d[star][en].data;
                break;
            }
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minn);
        }else printf("cannot reach destination\n");
    }
    return 0;
}
/*
/*
floyd+二分
Judge Status:Time Limit Exceeded
Exe.Time:10000MS
Exe.Memory:12212K
Code Len:1891 B
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1010;
const int M=1000010;
const int INF=0x3f3f3f3f;
struct node{
    int h;
    int data;
}dis[N][N];
int ans,minn,n,m,tot,star,en,sum;
node d[N][N];
queue<int>q;
void floyd(int x){
    memset(d,0x3f,sizeof(d));
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(dis[i][j].h>=x) d[i][j].data=dis[i][j].data;
            d[i][j].h=dis[i][j].h;
        }
    }
    for(int k=1;k<=n;++k){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if((d[i][j].data>d[i][k].data+d[k][j].data)&&(d[i][k].h>=x)&&(d[k][j].h>=x)){
                    d[i][j].data=d[i][k].data+d[k][j].data;
                    d[i][j].h=min(d[i][k].h,d[k][j].h);
                }
            }
        }
    }
}
int main(){
    while(scanf("%d %d",&n,&m)){
        if((!n)&&(!m)) return 0;
        if(sum) printf("\n");
        memset(dis,0,sizeof(dis));sum++;
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if(i==j) dis[i][j].data=0;
                else dis[i][j].data=INF;
            }
        }
        int l=0,r=0;
        for(int i=1;i<=m;++i){
            int x,y,z,h;
            scanf("%d %d %d %d",&x,&y,&h,&z);
            if(h==-1) h=INF;
            if(h>dis[x][y].h){
                dis[x][y].data=z;
                dis[x][y].h=h;
            }
            dis[y][x].data=dis[x][y].data;
            dis[y][x].h=dis[x][y].h;
        }scanf("%d %d %d",&star,&en,&r);
        ans=0;minn=0;
        while(l<=r){
            int mid=(l+r)>>1;
            floyd(mid);
            if(d[star][en].data!=INF){
                if(mid>ans){
                    ans=mid;
                    minn=d[star][en].data;
                } l=mid+1;
            }else r=mid-1;
        }
        printf("Case %d:\n",sum);
        if(ans!=0){
            printf("maximum height = %d\n",ans);
            printf("length of shortest route = %d\n",minn);
        }else printf("cannot reach destination\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/donkey2603089141/p/11748150.html

时间: 2024-10-11 18:40:19

hdu 2962 题解的相关文章

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

hdu 2962

二分加最短路 #include <stdio.h> #include <string.h> #define N 1005 #define INF 0x3f3f3f3f struct tt{ int h,cost; }dis[N][N]; int vis[N],d[N],num =1; int dijkstral(int v0,int t,int n,int h) { int i,temp,x,y; for(i = 1 ; i <= n ; i++) { if(dis[v0][

UVALive 4223 / HDU 2962 spfa + 二分

Trucking Problem Description A certain local trucking company would like to transport some goods on a cargo truck from one place to another. It is desirable to transport as much goods as possible each trip. Unfortunately, one cannot always use the ro

hdu - 1237 题解

题意:给出一个数字计算式,包含+,-,*,/四种符号,计算值 题解:最大坑点:不能仅仅判断第一个是0就结束计算,有可能有:0 + 1这样的情况 所以1.判断第一个是否是0,如果是,则判断下一个符号是否是'\n' 2.读入数字和运算符,如果是*,/,取出栈顶元素直接计算完成后压栈,如果是-,将数字相反数压栈,如果是+,将数字压栈. 3.计算栈内元素之和 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm>

2014年北京网络赛 Instrusive HDU 5040 题解 优先队列

网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点,若干墙,若干监视器,剩下的是空地. 起点,终点,监视器都算空地. 监视器初始值会指定一个方向,共有四个方向. 监视器每秒顺时针转动到下个方向. 监视器视野距离为2. 在监视器的位置或在监视器面向的格子是监视区域. 普通的移动一格需要消耗1秒时间. 在监视器下移动一格需要消耗3秒时间. 如果呆在原地不

hdu - 1231 题解

题意:最大连续子序列问题+输出答案所在的区间 题解:最大连续子序列问题状态转移方程:f[i]=max(a[i],f[i-1]+a[i]) 答案所在区间的话可以在递推求状态的时候,顺便记录一下当前位置所在的序列左端点是谁,最后扫描的时候记录下最优解的位置,然后这个位置就是右端点,记录过的数据就是左端点. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring&g

HDU 5573 题解

题意:关于一只要续命的青蛙(雾),一颗完全二叉树,它的标号正如我们所期望的根为1,左儿子为父节点*2,右儿子为父节点*2+1,然后老青蛙从根往下走,一共走K步,它需要n个灵魂,每走过一个点,可以减去或加上这个点的标号,输出一种可能方案,输入数据保证至少有一组解 1=<n<=1e9,N<=2k<=260 共1~100组数据,1000MS 题解/思路:其实这道题找一下规律就好了,你会发现最左边两条路径分别可以表示±1,±3,--,±(2k-1)和±2,±4,--,±2k,所以只需判断这

hdu 5418 题解

第一眼看到这题,哇,这不是我刚做完的题吗?大水题!然后 这题表面很水,实际上有点坑. 题意 求经过 $ 1 - n $(不能遗漏) 并且回到 $ 1 $ 的最短路. 在看这题之前我们可以来看下这题 最短Hamilton路 这道题的要求是我们要让每个点不重不漏的经过并且最终到达 $ n-1 $ 我们看数据范围,就可以直接状压dp,枚举状态. 由于题目已经给出最短路,便可以直接计算 #include<bits/stdc++.h> using namespace std; int n,a[30][3

HDU 5521 题解

题意:给出n个点,John需要和一个住在第n个点的人在某个点碰面,再给出m个集合,每个集合中包含Si个点,这些点两两之间可以以ti时间互达,求两人需花费的最小时间,若无法走到第n个点,则输出Evil John. 2<=N<=100000;1<=ti<=1e9;Si>0;∑Si<=1e6; 共1~6组数据,6000MS 算法/思路: 第一次见到这种套路的话还是蛮有趣的,对每个集合中两两点建边显然会被轻松TLE. 使用最短路+虚拟节点:于是我们对每个集合建立一个虚拟节点,在