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

题目链接

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 become 00. Now she wants to go to City NN, please help her calculate the minimum distance.

Input

The first line has one integer T(1 \le T\le 5)T(1≤T≤5), then following TT cases.

For each test case, the first line has three integers N, MN,M and KK.

Then the following MM lines each line has three integers, describe a road, U_i, V_i, C_iUi​,Vi​,Ci​. There might be multiple edges between uu and vv.

It is guaranteed that N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0 \le C_i \le 1e90≤Ci​≤1e9. There is at least one path between City 11 and City NN.

Output

For each test case, print the minimum distance.

样例输入

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

样例输出

3

  n个点m条边,允许将<=k条边的权值更改为0,问从1到n的最短路。

  思路:

  最短路加一维维护当前删除边的条数。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=200010;
const ll inf=1e18;


int n,m,k,head[N],cas;
ll dist[N][15],ans,x,y,z;
int vis[N][15];

struct Edge
{
    int u,v,next,w;
}edge[N];

struct Node
{
    int idx,num;//到idx的距离,更改了多少条边的权值
    ll dis;//到idx的距离
    bool operator < (const Node &other) const
    {
        return dis<other.dis;
    }
}now,nex,one;

void add_edge(int u,int v,int w)
{
    edge[cas].u=u;
    edge[cas].v=v;
    edge[cas].w=w;
    edge[cas].next=head[u];
    head[u]=cas++;
}

multiset<Node>s;//按照距离从小到大排序,距离小的有限访问
multiset<Node>::iterator it;

void dij()
{
    int i,j,v;
    memset(vis,0,sizeof(vis));
    for(i=0;i<=n;i++)
        for(j=0;j<=k;j++)
            dist[i][j]=inf;
    s.clear();
    dist[1][0]=0;
    one.dis=0,one.idx=1,one.num=0;
    s.insert(one);
    while(!s.empty())
    {
        it=s.begin();
        now=*it;
        s.erase(it);
        if(vis[now.idx][now.num])//如果已经更新过,最短,不需要再处理
            continue;
        vis[now.idx][now.num]=1;
        for(i=head[now.idx];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;
            if(!vis[v][now.num]&&dist[v][now.num]>now.dis+edge[i].w)//不删除当前边,正常更新
            {
                dist[v][now.num]=now.dis+edge[i].w;
                nex.dis=dist[v][now.num];
                nex.idx=v;
                nex.num=now.num;
                s.insert(nex);
            }
            if(now.num<k&&dist[v][now.num+1]>now.dis)//将从now.idx到v的边设为0
            {
                dist[v][now.num+1]=now.dis;
                nex.dis=now.dis;
                nex.idx=v;
                nex.num=now.num+1;
                s.insert(nex);
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,j;
        cas=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d%d",&n,&m,&k);
        for(i=1;i<=m;i++)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            add_edge(x,y,z);
        }
        dij();
        ans=inf;
        for(i=0;i<=k;i++)
            ans=min(ans,dist[n][i]);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinghui_7/article/details/82318407