2019 牛客暑期多校训练营(第四场)J Free

J Free

链接:https://ac.nowcoder.com/acm/contest/884/J

Problem

Your are given an undirect connected graph.Every edge has a cost to pass.You should choose a path from S to T and you need to pay for all the edges in your path. However, you can choose at most k edges in the graph and change their costs to zero in the beginning. Please answer the minimal total cost you need to pay.

输入描述:

The first line contains five integers n,m,S,T,K.

For each of the following m lines, there are three integers a,b,l, meaning there is an edge that costs l between a and b.

n is the number of nodes and m is the number of edges.

输出描述:

An integer meaning the minimal total cost.

示例1

输入
3 2 1 3 1
1 2 1
2 3 2
输出
1

备注:

1 <= n,m <= 103,1 <= S,T,a, b <= n, 0 <= k <= m,1 <= 106
.
Multiple edges and self loops are allowed.

题意

在一个无向图上,问起点S到终点T的最短路径,其中最多有k次机会可以直接通过一条边而不计算边权

分析

迪杰斯特拉算法
dis[i][j]表示到达i点,已经改了j次边权,此时的最短路。

相当于将原图复制成了k层,每改变一次,就向下走一层。

两种情况(如果可以变优):

(1)不用变0技能:转移到dis[dest][j] = dis[now][j] + len

(2)用变0技能:转移到dis[dest][j+1] = dis[now][j]

还有此题卡spfa,要用dijkstra。

因为dijkstra每次处理的点,最小值都已经确定。

所以第一次now.idx == n的时候,now.dis即为答案。

代码

#include 
#include 
#include 
#include 
#define MAX_N 10005
#define MAX_K 25

using namespace std;

struct Edge
{
    int dest;
    int len;
    Edge(int _dest,int _len)
    {
        dest=_dest;
        len=_len;
    }
    Edge(){}
};

struct Node
{
    int idx;
    int cnt;
    int dis;
    Node(int _idx,int _cnt,int _dis)
    {
        idx=_idx;
        cnt=_cnt;
        dis=_dis;
    }
    Node(){}
    friend bool operator < (const Node &a,const Node &b)
    {
        return a.dis>b.dis;
    }
};

int n,m,k;
int ans;
int dis[MAX_N][MAX_K];
vector edge[MAX_N];
priority_queue q;

void read()
{
    cin>>n>>m>>k;
    int a,b,v;
    for(int i=0;i>a>>b>>v;
        edge[a].push_back(Edge(b,v));
        edge[b].push_back(Edge(a,v));
    }
}

int dijkstra(int start,int dst)
{
    memset(dis,0x3f,sizeof(dis));
    q.push(Node(start,0,0));
    dis[start][0]=0;
    while(!q.empty())
    {
        Node now=q.top();
        q.pop();
        if(now.idx==dst) return now.dis;
        if(dis[now.idx][now.cnt]now.dis+temp.len)
            {
                dis[temp.dest][now.cnt]=now.dis+temp.len;
                q.push(Node(temp.dest,now.cnt,dis[temp.dest][now.cnt]));
            }
            if(dis[temp.dest][now.cnt+1]>now.dis && now.cnt+1<=k)
            {
                dis[temp.dest][now.cnt+1]=now.dis;
                q.push(Node(temp.dest,now.cnt+1,dis[temp.dest][now.cnt+1]));
            }
        }
    }
}

void solve()
{
    ans=dijkstra(1,n);
}

void print()
{
    cout<

你可能感兴趣的:(acm)