首页 > 其他分享 >AcWing 第 94 场周赛 A B(最短路dij) C(最短路floyed)

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

时间:2023-03-12 18:23:56浏览次数:49  
标签:周赛 const idx dij int 短路 样例 dist LL

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;
}

标签:周赛,const,idx,dij,int,短路,样例,dist,LL
From: https://www.cnblogs.com/Vivian-0918/p/17208672.html

相关文章

  • 最短路之和
    最短路之和给定一个$n$个点的加权有向图,点的编号为$1\simn$。图中任意两点之间都有两条方向相反的边连接两点。从点$i$到点$j$的边的长度为$a_{ij}$。给定一......
  • AcWing94场周赛复盘
    快速手速题不能犹豫已知,每个背包最多可以装件物品。请你计算,要装下件物品最少需要多少个背包。输入格式一个整数。输出格式一个整数,表示所需背包的最少数量。数据范围......
  • 6317.统计美丽子数组的数目-336周赛
    统计美丽子数组的数目给你一个下标从0 开始的整数数组nums 。每次操作中,你可以:选择两个满足 0<=i,j<nums.length 的不同下标 i 和 j 。选择一个非负整数......
  • 最短路径算法
    原理1.算出目前数据中,起点到终点的最短路径2.路径从短到长获取目前最短的路径,设置标识,有标识的不参与下一步循环 packagecom.jason.base.arithmetic;importlom......
  • LeetCode 周赛 335,纯纯手速场!
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。昨晚是LeetCode第335场周赛,你参加了吗?这场周赛整体难度不高,有两道模板题......
  • [第335场周赛]质因数分解,多重背包
    喜提AK以及最佳排名:6307. 递枕头提示简单3相关企业​​n​​ 个人站成一排,按从 ​​1​​ 到 ​​n​​ 编号。最初,排在队首的第一个人拿着一个枕头。每秒钟,拿着枕头......
  • 力扣第335场周赛补题题解
    目录1.递枕头2.二叉树中的第K大层和3.分割数组使乘积互质4.获得分数的方法数1.递枕头classSolution{public:intpassThePillow(intn,inttime){......
  • Acwing 93周赛 C
    异或值(字典树)思路唉,人太笨了,知道用字典树,但想不出过程,知其然而不知其所以然。代码voidinsert(intx){ intp=0; for(inti=30;i>=0;i--) { intu=(x>>i)&......
  • 周赛_ABC291
    C-LRUDInstructions2题面说了这样一句:(includingthestartingandendingpoints)我不以为意捏,认为怎么会错过。结果WA了一发。回头去找别人做的,似乎也只是把我用......
  • dijkstra 建立虚拟源点求最短路
    题目:有N个村庄,编号1到N。村庄之间有M条无向道路,第i条道路连接村庄ai和村庄bi,长度是ci。所有村庄都是连通的。共有K个村庄有商店,第j个有商店的村庄编......