ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)

题目链接https://nanti.jisuanke.com/t/31001

题目大意:给出一个含有n个点m条边的带权有向图,求1号顶点到n号顶点的最短路,可以使<=k条任意边的权值变为0。

样例输入 复制

1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2

样例输出 复制

3

解题思路:可以用两种做法,不过都差不多,应该算是同一种思路的不同写法。第一种是在建图时,将一个点拆成k个层次的点,应该总共有k+1层,每个相同层次的点按输入的边权连接,每个点可以向它能连接到的点的下一个层次连接一条边权为0的边,这样你每使一条边权值变为0,即相当于走到了下一层图,永远不能走回,当走到第k+1层图时即不能使用了,在这个含有k+1层图的大图里跑下最短路就可以得出答案了附上代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2200007;
const ll inf=0x3f3f3f3f;
struct qnode{
    int u;
    ll dis;
    bool operator<(const qnode &a)const{
        return dis>a.dis;
    }
    qnode(int a,ll b)
    {
        u=a;
        dis=b;
    }
};
struct node{
    int v,w,next;
}edge[2*maxn];
int n,m,k,tot=0,head[maxn];
ll dis[maxn];
void add(int u,int v,int w)
{
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void dij()
{
    priority_queue<qnode> que;
    memset(dis,inf,sizeof(dis));
    dis[1]=0;
    que.push(qnode(1,0));
    while(!que.empty())
    {
        int u=que.top().u;
        que.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                que.push(qnode(v,dis[v]));
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            for(int j=0;j<=k;j++)
            {
                add(u+j*n,v+j*n,w); //同一层次的点用输入边权相连
                if(j!=k)
                    add(u+j*n,v+(j+1)*n,0); //不同层次的点用0权值相连
            }
        }
        if(k>=m)
        {
            printf("0\n");
            continue;
        }
        ll ans=inf;
        dij();
        for(int i=0;i<=k;i++)
            ans=min(ans,dis[n+i*n]);
        printf("%lld\n",ans);
    }
    return 0;
}

第二种是用最短路+dp思想,再开一维数组记录已经使用了几次使边的权值为0,也是跑下最短路就可以了。

附上代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
const long long inf=0x3f3f3f3f;
struct node{
    int v,w,next;
}edge[2*maxn];
struct qnode{
    int u,k;
    long long dis;
    bool operator<(const qnode &a)const{
        return dis>a.dis;
    }
    qnode(int a,int b,long long c)
    {
        u=a;
        k=b;
        dis=c;
    }
};
int n,m,k,tot=0,head[maxn];
long long dis[maxn][15];
void add(int u,int v,int w)
{
    edge[tot].v=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
long long dijkstra()
{
    priority_queue<qnode> que;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=k;j++)
        {
            dis[i][j]=inf;
        }
    }
    que.push(qnode(1,0,0));
    dis[1][0]=0;
    while(!que.empty())
    {
        int u=que.top().u,tempk=que.top().k;
        if(u==n)
            return dis[u][tempk];
        que.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(dis[u][tempk]+w<dis[v][tempk])  //同一层的最短路
            {
                dis[v][tempk]=dis[u][tempk]+w;
                que.push(qnode(v,tempk,dis[v][tempk]));
            }
            if(tempk<k)
            {
                if(dis[u][tempk]<dis[v][tempk+1])  //如果将这条边权值变为0,就会进入tempk+1层
                {
                    dis[v][tempk+1]=dis[u][tempk];
                    que.push(qnode(v,tempk+1,dis[v][tempk+1]));
                }
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        init();
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        printf("%lld\n",dijkstra());
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zjl192628928/p/9744404.html

时间: 2024-11-10 17:24:58

ACM-ICPC 2018 南京赛区网络预赛 L题(分层最短路)的相关文章

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

262144K There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici?. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze(分层dijkstra)

题意:有n个地方,m条带权值的路,你有k次机会将一条路的权值变为0,求1到n的最短距离. 分析:这是一题分层dijkstra的模板题,因为k的大小不是很大,最坏的情况也就是10,而我们一般的最短路问题只是建一个平面的图. 而分层dijkstra是一个空间的.怎么操作呢? 举个简单的栗子 当数据如下是 n  m  k 3  2  1 1  2  5(路的起点1,终点2,权值5) 2  3  6 没有k的情况就是这样的 如果有k的情况,即1到2权值可能为0,2到3也可能为0,我们可以加一个平面表示他

ACM-ICPC 2018 南京赛区网络预赛 做题记录

比赛的时候被J题卡常然后B题自闭最后G题没调完 5题gg 现在补题进度:6/12 A. 题解: 高考数学题,裂项完之后答案就是n!-1 (n!)%n=0,所以就是n-1 1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int T; 5 ll n; 6 int main() 7 { 8 cin>>T; 9 while(T--) 10 { 11 cin>>n; 12 cout

ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树

目录 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 题意 思路 ACM-ICPC 2018 南京赛区网络预赛 Lpl and Energy-saving Lamps 线段树 题面 During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the

ACM-ICPC 2018 南京赛区网络预赛 J.Sum

Sum A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into

计蒜客 ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem-数学公式题

A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him.

ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring wonderful substring when the times it appears in that string is between AA and BB (A \le times \le BA≤times≤B). Can you calculate the number of wonderful