AcWing 第 94 场周赛 A B(最短路dij) C(最短路floyed)

https://www.acwing.com/activity/content/competition/problem_list/2961/

AcWing 4870. 装物品

水题

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        LL sum=n/5;
        if(n%5!=0) sum++;
        cout<<sum<<endl;
    }
    return 0;
}

AcWing 4871. 最早时刻

输入样例1:
4 6
1 2 2
1 3 3
1 4 8
2 3 4
2 4 5
3 4 3
0
1 3
2 3 4
0
输出样例1:
7

板子题,拿堆优化版dij改动一下就行了
注意源点

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,m;
LL h[N],w[N],dist[N],e[N],ne[N],idx=0;
map<PII,LL> mp;
bool st[N];
void add(int x,int y,int z)
{
    e[idx]=y,w[idx]=z,ne[idx]=h[x],h[x]=idx++;
}
int dijkstra()
{
    memset(dist,0x3f,sizeof dist);
    dist[1]=0;
    priority_queue<PII,vector<PII>,greater<PII>> heap;
    LL idx1=dist[1];
    while(mp[{idx1,1}]!=0)
    {
        idx1++;
    }
    heap.push({idx1,1});
    while(heap.size())
    {
        auto t=heap.top();
        heap.pop();
        int ver=t.second,distance=t.first;
        if(st[ver]) continue;
        st[ver]=true;
        for(int i=h[ver];i!=-1;i=ne[i])
        {
            int j=e[i];
            if(dist[j]>distance+w[i])
            {
                dist[j]=distance+w[i];
                //heap.push({dist[j],j});
                LL idx=dist[j];
                while(mp[{idx,j}]!=0)
                {
                    idx++;
                }
                heap.push({idx,j});
            }
        }
    }
    if(dist[n]>=0x3f3f3f3f) return -1;
    return dist[n];
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        memset(h,-1,sizeof h);
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            LL x,y,z;
            cin>>x>>y>>z;
            add(x,y,z);
            add(y,x,z);
        }
        //cout<<dijkstra()<<endl;
        for(int i=1;i<=n;i++)
        {
            LL op;
            cin>>op;
            while(op--)
            {
                LL x;
                cin>>x;
                mp[{x,i}]++;//在第i个点上第x个时刻不可以动
            }
        }
        cout<<dijkstra()<<endl;
    }
    return 0;
}

AcWing 4872. 最短路之和

输入样例1:
1
0
1
输出样例1:
0
输入样例2:
2
0 5
4 0
1 2
输出样例2:
9 0
输入样例3:
4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3
输出样例3:
17 23 404 0

学习了一下最高点击率题解
传送门:https://www.acwing.com/solution/content/175799/

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,a[M][M],b[N],ans[N];
bool st[N];
void floyed(LL x)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            a[i][j]=min(a[i][j],a[i][x]+a[x][j]);
        }
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>a[i][j];
            }
        }
        for(int i=1;i<=n;i++)
            cin>>b[i];
        for(int k=n;k>=1;k--)
        {
            st[b[k]]=true;
            floyed(b[k]);
            for(int i=1;i<=n;i++)
            {
                if(!st[i]) continue;
                for(int j=1;j<=n;j++)
                {
                    if(st[j]) ans[k]+=a[i][j];
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
posted @ 2023-03-12 18:10  Vijurria  阅读(13)  评论(0编辑  收藏  举报