E. Weights Distributing(图论,贪心)
题目传送门:
题目大意:
有n个点,m条路径,每次经过一条路径时都需要一次该路径的花费。有一个花费数组对应着每一条路径,要求给出一个权值分配方案使得从点a->b->c的花费最小为多少。
思路:
1.假设a->b之后b->c没有经过之前经过的点,那么也就是三点之间经过的路径是一条直线。
2.否则b->c经过了之前经过的x,那么也就有了a->x->b->x->c的路径。
那么b->x这一段路径经过了两次,所以我们优先给这一段路径分配权值,然后再为另外两段路径分配权值。
先求出a,b,c三点到其他点的最短距离,然后将花费数组贪心之后用前缀和优化一下即可。
AC Code
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10;
int dis[4][N];//a,b,c三个点到每个点的距离
LL sum[N],price[N];
vector<int>g[N];
struct node
{
int to,dist;
};
priority_queue<node>que;
bool operator<(const node &p,const node &q)
{
return p.dist>q.dist;
}
void dijkstra(int idx,int num)
{
dis[idx][num]