2019icpc Nanchang network game

 


 

 

B. Fire-Fighting Hero (dijstra priority queue + bfs)

Meaning of the title: the beginning of the wrong problem, that is k times dijkstra, but wa, and later discovered quite correct teammates after water. S to find a maximum value of the shortest ans1 other points, and other points to find the shortest path to the specified one of the k point maximum ans2. Compare ans1 and ans2 can be.

Ideas: with dijstra optimization queue seeking ans1, k times bfs seeking ans2 priority queue can be.

AC code:

#include<cstdio>
#include<algorithm>
#include<cctype>
#include<queue>
using namespace std;

inline int read()
{
    int x=0,f=0; char ch=0;
    while(!isdigit(ch)) {f|=ch=='-';ch=getchar();}
    while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int maxn=1005;
const int maxm=5e5+5;
const int inf=0x3f3f3f3f;
int T,n,m,s,k,c,cnt,head[maxn],isk[maxn],dist[maxn],vis[maxn];
int ans1,ans2;

struct node1{
    int v,w,nex;
}edge[maxm];

void adde(int u,int v,int w){
    edge[++cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].nex=head[u];
    head[u]=cnt;
}

struct node2{
    int w,id;
    node2(){}
    node2(int w,int id):w(w),id(id){}
};

bool operator < (const node2& a,const node2& b){
    return a.w>b.w;
}

void dijkstra(int s)
{
    priority_queue<node2> que;
    for(int i=1; i<=n; i++)
        dist[i]=inf,vis[i]=0;
    dist[s]=0;
    que.push(node2(0,s));
    while(!que.empty())
    {
        int u=que.top().id;
        que.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=head[u];i;i=edge[i].nex) 
        {
            int v=edge[i].v;
            int w=edge[i].w;
            if(dist[v]>dist[u]+w)
            {
                dist[v]=dist[u]+w;
                node2 rec;
                rec.id=v;
                rec.w=dist[v];
                que.push(rec);
            }
        }
    }
}

int bfs(int s){
    priority_queue<node2> que;
    for(int i=1; i<=n; ++i)
        vis[i]=0;
    que.push(node2(0,s));
    while(!que.empty()){
        node2 now=que.top();que.pop();
        int nid=now.id,nw=now.w;
        if(vis[nid]) continue;
        vis[nid]=1;
        if(isk[nid])
            return nw;
        for(int i=head[nid];i;i=edge[i].nex){
            int v=edge[i].v;
            int w=edge[i].w;
            que.push(node2(nw+w,v));
        }
    }
}

int main(){
    T=read();
    while(T--){
        n=read(),m=read(),s=read(),k=read(),c=read();
        cnt=0;
        ans1=ans2=0;
        for(int i=1;i<=n;++i)
            head[i]=0,isk[i]=0;
        for(int i=1;i<=k;++i)
            isk[read()]=1;
        for(int i=1;i<=m;++i){
            int u=read(),v=read(),w=read();
            adde(u,v,w);
            adde(v,u,w);
        }
        dijkstra(s);
        for(int i=1;i<=n;++i)
            ans1=max(ans1,dist[i]);
        for(int i=1;i<=n;++i){
            if(isk[i]) continue;
            ans2=max(ans2,bfs(i));
        }
        if(ans1<=c*ans2) printf("%d\n",ans1);
        else printf("%d\n",ans2);
    }
    return 0;
}
View Code

 

 


 

 

E. Magic Master (violence)

Meaning of the questions: Analog.

Ideas: According to the meaning of problems can be simulated backwards, not violent game, will be looked at hyperspace time, proved to be dared to take the game to try.

AC code:

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int T,n,m,Q;
struct node{
    int q,id,ans;
}query[105];

bool cmp1(const node& a,const node& b){
    return a.q>b.q;
}

bool cmp2(const node& a,const node& b){
    return a.id<b.id;
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&Q);
        for(int i=1;i<=Q;++i){
            scanf("%d",&query[i].q);
            query[i].id=i;
        }
        sort(query+1,query+1+Q,cmp1);
        queue<int> que;
        for(int i=n;i>=1;--i){
            if(!que.empty()){
                for(int j=1;j<=m;++j){
                    int tmp=que.front();que.pop();
                    que.push(tmp);
                }
            }
            que.push(i);
        }
        int cnt=1;
        for(int i=n;i>=1;--i){
            int tmp=que.front();que.pop();
            if(i==query[cnt].q)
                query[cnt++].ans=tmp;
            if(cnt>Q) break;
        }
        sort(query+1,query+1+Q,cmp2);
        for(int i=1;i<=Q;++i)
            printf("%d\n",query[i].ans);
    }
    return 0;
}
View Code

 


 

 

G. Pangu Separates Heaven and Earth (sign title)

The meaning of problems: water problems, according to the required output.

AC code:

#include<cstdio>
#include<algorithm>
using namespace std;

int T;
int a;

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&a);
        if(a==1) printf("18000\n");
        else printf("0\n");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11491063.html
Recommended