题解 图论题
发现能力只能用一次
首先有一个分层图的思路,但是边建不下
然后发现可以用两次增广代替分层图
先跑一次最短路,然后拆式子建立凸包
更新每个点的 dis 数组后再重新增广一次即可
其实这个东西是有决策单调性的
- 貌似和代价为什么东西的平方相关的问题几乎都有决策单调性?
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 400010
#define ll long long
#define fir first
#define sec second
#define pb push_back
#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m, s, t;
ll dis[N];
bool vis[N];
int head[N], size, top;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
struct edge{int to, next; ll val;}e[N<<1];
inline void add(int s, int t, int w) {e[++size]={t, head[s], w}; head[s]=size;}
namespace force{
void dij() {
memset(dis, 127, sizeof(dis));
memset(vis, 0, sizeof(vis));
while (q.size()) q.pop();
dis[s]=0; q.push({0, s});
while (q.size()) {
pair<ll, int> u=q.top(); q.pop();
if (vis[u.sec]) continue;
vis[u.sec]=1;
for (int i=head[u.sec],v; ~i; i=e[i].next) {
v = e[i].to;
if (dis[v]>dis[u.sec]+e[i].val) {
dis[v]=dis[u.sec]+e[i].val;
q.push({dis[v], v});
}
}
}
}
void solve() {
for (int i=1,u,v,w; i<=m; ++i) {
u=read(); v=read(); w=read();
add(u, v, w);
add(u+n, v+n, w);
}
for (int i=1; i<=n; ++i)
for (int j=1; j<=n; ++j)
add(i, j+n, 1ll*(i-j)*(i-j));
dij();
printf("%lld\n", dis[t+n]);
exit(0);
}
}
namespace task1{
struct line{ll k, b; double p;}sta[N];
vector<line> tem;
inline bool operator < (line a, line b) {return a.k>b.k;}
inline bool operator < (line a, double b) {return a.p<b;}
struct convex{
const double inf=1e30;
vector<line> a;
inline double qmeet(line a, line b) {return 1.0*(b.b-a.b)/(a.k-b.k);}
void build(vector<line>& s) {
a.clear(); top=0;
sort(s.begin(), s.end());
for (auto it:s) {
while (top>1 && qmeet(sta[top-1], sta[top])>=qmeet(sta[top], it)) --top;
sta[top].p=qmeet(sta[top], it); sta[++top]=it;
}
sta[top].p=inf;
for (int i=1; i<=top; ++i) a.pb(sta[i]);
}
ll query(double x, ll x2) {
auto it=lower_bound(a.begin(), a.end(), x);
return it->k*x2+it->b;
}
}hull;
void dij1() {
memset(dis, 127, sizeof(dis));
memset(vis, 0, sizeof(vis));
while (q.size()) q.pop();
dis[s]=0; q.push({0, s});
while (q.size()) {
pair<ll, int> u=q.top(); q.pop();
if (vis[u.sec]) continue;
vis[u.sec]=1;
for (int i=head[u.sec],v; ~i; i=e[i].next) {
v = e[i].to;
if (dis[v]>dis[u.sec]+e[i].val) {
dis[v]=dis[u.sec]+e[i].val;
q.push({dis[v], v});
}
}
}
}
void dij2() {
memset(vis, 0, sizeof(vis));
while (q.size()) q.pop();
for (int i=1; i<=n; ++i) q.push({dis[i], i});
while (q.size()) {
pair<ll, int> u=q.top(); q.pop();
if (vis[u.sec]) continue;
vis[u.sec]=1;
for (int i=head[u.sec],v; ~i; i=e[i].next) {
v = e[i].to;
if (dis[v]>dis[u.sec]+e[i].val) {
dis[v]=dis[u.sec]+e[i].val;
q.push({dis[v], v});
}
}
}
}
void solve() {
for (int i=1,u,v,w; i<=m; ++i) {
u=read(); v=read(); w=read();
add(u, v, w);
}
dij1();
for (int i=1; i<=n; ++i) tem.pb({-2*i, 1ll*i*i+dis[i]});
hull.build(tem);
for (int i=1; i<=n; ++i) dis[i]=min(dis[i], 1ll*i*i+hull.query(i, i));
dij2();
printf("%lld\n", dis[t]);
exit(0);
}
}
signed main()
{
freopen("graph.in", "r", stdin);
freopen("graph.out", "w", stdout);
n=read(); m=read(); s=read(); t=read();
memset(head, -1, sizeof(head));
// force::solve();
task1::solve();
return 0;
}