CSU 1891: Full Tank? 1892: Nested Dolls 1897: The price table of the snack store

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nameofcsdn/article/details/80317250

1891: Full Tank?

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
const int maxm=10010,maxn=1010,maxc=110;

struct Edge{
    int v,nt,c;
    explicit Edge(int a=0,int b=0,int cc=0):v(a),nt(b),c(cc){};
}edge[maxm<<1];
struct Node{
    int cost,city,fuel;
    explicit Node(int a=0,int b=0,int c=0):cost(a),city(b),fuel(c){};
    bool operator<(const Node& n)const{return cost>n.cost;}
}tmp;

int n,m,fi[maxn],p[maxn],x,y,c,ind,q,s,e,co[maxn][maxc];
bool vis[maxn][maxc];
priority_queue<Node> pq;
inline void addeage(){
    edge[ind]=Edge(x,fi[y],c);
    fi[y]=ind++;
    edge[ind]=Edge(y,fi[x],c);
    fi[x]=ind++;
}

int main(){
    ios_base::sync_with_stdio(0);
    while(cin>>n>>m){
        memset(fi,-1,sizeof(int)*(n+1));
        for(int i=0;i<n;++i)
            cin>>p[i];
        for(int i=0;i<m;++i){
            cin>>x>>y>>c;
            addeage();
        }
        cin>>q;
        while(q--){
            cin>>c>>s>>e;
            memset(co,0x3f3f3f3f,sizeof(co[0])*(n+1));
            memset(vis,0,sizeof(vis[0])*(n+1));
            co[s][0]=0;
            pq.push(Node(0,s,0));
            while(!pq.empty()){
                tmp=pq.top();pq.pop();
                if(vis[tmp.city][tmp.fuel])
                    continue;
                if(tmp.city==e)
                    break;
                if(tmp.fuel<c&&tmp.cost+p[tmp.city]<co[tmp.city][tmp.fuel+1]){
                    co[tmp.city][tmp.fuel+1]=tmp.cost+p[tmp.city];
                    pq.push(Node(tmp.cost+p[tmp.city],tmp.city,tmp.fuel+1));
                }
                for(int i=fi[tmp.city];i!=-1;i=edge[i].nt)
                if(tmp.fuel-edge[i].c>=0&&co[tmp.city][tmp.fuel]<co[edge[i].v][tmp.fuel-edge[i].c]){
                    co[edge[i].v][tmp.fuel-edge[i].c]=co[tmp.city][tmp.fuel];
                    pq.push(Node(co[edge[i].v][tmp.fuel-edge[i].c],edge[i].v,tmp.fuel-edge[i].c));
                }
                vis[tmp.city][tmp.fuel]=true;
            }
            if(co[e][0]<0x3f3f3f3f)
                cout<<co[e][0]<<endl;
            else
                cout<<"impossible\n";
            while(!pq.empty())pq.pop();
        }
        ind=0;
    }
    return 0;
}

1892: Nested Dolls

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Doll{
       int width,height;
}doll[20005],selected[20005];
bool cmp(Doll a,Doll b){
     if(a.width==b.width)
         return a.height>b.height; //这里只可用 >,用 < WA了 
     return a.width<b.width;
}
int main(){
    int t,n,cnt;
    scanf("%d",&t);
    while(t--){
         scanf("%d",&n);
         for(int i=0;i<n;i++)
              scanf("%d%d",&doll[i].width,&doll[i].height);
         sort(doll,doll+n,cmp);
         cnt=0;
         for(int i=0;i<n;i++){
              int left=0,right=cnt,mid;
              while(left<right){
                    mid=(left+right)/2; 
                    if(doll[i].width<=selected[mid].width || 
                           doll[i].height<=selected[mid].height)
                           left=mid+1;
                    else
                           right=mid;               
              }        
              if(left==cnt)
                   selected[cnt++]=doll[i];
              else
                   selected[left]=doll[i];                             
         }   
         printf("%d\n",cnt);  
               
    }
return 0;
}

1897: The price table of the snack store

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long LL;
int L[10005];
LL c[10005],x[10005];

int main()
{
    int T;
    scanf("%d",&T);
    LL sigmax;
    while(T--)
    {
        int k,n,m;
        scanf("%d%d%d",&k,&n,&m);
        sigmax=0;
        for(int i=1;i<=k;i++)
        {
            scanf("%lld%d",&c[i],&L[i]);
            sigmax+=c[i];
        }
        sigmax=sigmax/(n+(k-1)*m);
        for(int i=1;i<=k;i++)
            x[L[i]]=(c[i]-sigmax*m)/(n-m);
        for(int i=1;i<=k;i++)
        {
            if(i==1) printf("%lld",x[i]);
            else printf(" %lld",x[i]);
        }
        printf("\n");
    }
    return 0;
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页