---
id: 2342
lang: en
translation_of: index.md
source_sha256: e86d13ae43d3f5358968b78ccb714c848b7412ea1ee228016e0ff8b1dd0888ba
title: "P3547: [POI2013] CEN-Price List — Solution"
description: "Consider three forms of shortest path. The first two need just one BFS; the third requires that no outside edge forms a triangle with two edges of the path."
tags: []
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "三元环 (three-element cycle) rendered as “triangle”."
  - "假的 (fake) rendered as “bogus” without a chip, as in posts/2020/2111-libreoj3144."
---

## Main Idea

Consider three forms of shortest path: $A \times dist, \lfloor \frac{dist}{2} \rfloor B + (dist \bmod 2) A, kB$. The first two just take one BFS. The third has a special requirement on the path: there must be no edge outside the edge set that forms a triangle with some two edges of the path.

Speaking of triangles, we can imitate that square-root method: mark first, then enumerate the middle edge. However, here we can't split the edges into one-way edges, so doing the square-root method directly is bogus.

Thinking further about a triangle $(u, v, w)$, we find that if we've found such a triangle, at the very least we can just delete $(v, w)$, because this edge won't play any role anymore (because the BFS is certainly giving the optimal solution).

<!--more-->

## Code

```cpp
// P3547.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200, INF = 0x3f3f3f3f;

int n, m, K, A, B, phead[MAX_N], rhead[MAX_N], current, dist[MAX_N];
int ans[MAX_N];
bool tag[MAX_N];

struct edge
{
    int to, nxt, pre;
} edges[MAX_N << 2];

void addpath(int *head, int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src], edges[current].pre = -1;
    if (edges[current].nxt != -1)
        edges[edges[current].nxt].pre = current;
    head[src] = current++;
}

void remove(int *head, int u, int eid)
{
    if (edges[eid].pre != -1)
        edges[edges[eid].pre].nxt = edges[eid].nxt;
    if (edges[eid].nxt != -1)
        edges[edges[eid].nxt].pre = edges[eid].pre;
    if (head[u] == eid)
        head[u] = edges[eid].nxt;
}

int main()
{
    memset(phead, -1, sizeof(phead)), memset(rhead, -1, sizeof(rhead));
    scanf("%d%d%d%d%d", &n, &m, &K, &A, &B);
    for (int i = 1, u, v; i <= m; i++)
    {
        scanf("%d%d", &u, &v);
        addpath(phead, u, v), addpath(phead, v, u);
        addpath(rhead, u, v), addpath(rhead, v, u);
    }
    memset(dist, -1, sizeof(dist)), memset(ans, 0x3f, sizeof(ans));
    queue<int> q;
    q.push(K), dist[K] = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = phead[u]; i != -1; i = edges[i].nxt)
            if (dist[edges[i].to] == -1)
                dist[edges[i].to] = dist[u] + 1, q.push(edges[i].to);
    }
    for (int i = 1; i <= n; i++)
    {
        if (dist[i] == -1)
            ans[i] = INF;
        else
            ans[i] = min(dist[i] * A, (dist[i] >> 1) * B + (dist[i] & 1) * A);
        dist[i] = -1;
    }
    q.push(K), dist[K] = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = phead[u]; i != -1; i = edges[i].nxt)
            tag[edges[i].to] = true;
        for (int i = phead[u]; i != -1; i = edges[i].nxt)
            for (int j = rhead[edges[i].to]; j != -1; j = edges[j].nxt)
                if (dist[edges[j].to] == -1 && tag[edges[j].to] == false)
                {
                    dist[edges[j].to] = dist[u] + 1, remove(rhead, edges[i].to, j);
                    q.push(edges[j].to);
                }
        for (int i = phead[u]; i != -1; i = edges[i].nxt)
            tag[edges[i].to] = false;
    }
    for (int i = 1; i <= n; i++)
    {
        if (dist[i] != -1)
            ans[i] = min(ans[i], dist[i] * B);
        dist[i] = -1;
    }
    for (int i = 1; i <= n; i++)
        printf("%d\n", ans[i]);
    return 0;
}
```
