[Explanations] [SDOI2010] continental hegemony

Face questions

answer

For a point to \ (i \) , we have two conditions

Arrival \ (I \) minimum time point, with \ (dis1_i \) represents

Complete destruction of all protection \ (i \) minimum time point of the city

Whichever \ (max \) i.e. to \ (I \) point minimum time

For the destruction of the protective point in the city, in a similar manner to the topological order

Code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define N 200005
using namespace std;

int n, m, in[N], dis1[N], dis2[N]; 
struct edge { int to, next, cost; }; 
struct dist { int num, dis; bool operator < (const dist &p) const { return dis > p.dis; } }; 
struct Graph
{
    edge e[N << 1]; int head[N]; int cnt;
    Graph() { cnt = 0; }
    inline void adde(int u, int v, int w) { e[++cnt] = (edge) { v, head[u], w }; head[u] = cnt; } 
} A, B; 
bool vis[N]; 

namespace Heap
{
    dist heap[N << 2]; int sz = 0;
    void push(dist x) { heap[++sz] = x; push_heap(heap + 1, heap + sz + 1); }
    void pop() { pop_heap(heap + 1, heap + sz + 1); sz--; }
    dist top() { return heap[1]; }
    bool empty() { return !sz; }
};

using namespace :: Heap; 

inline int read()
{
    int x = 0, w = 1; char c = getchar();
    while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * w;
}

void dijkstra()
{
    memset(dis1, 0x3f, sizeof(dis1));
    memset(vis, 0, sizeof(vis)); 
    dis1[1] = 0; push((dist) { 1, 0 }); 
    while(!empty())
    {
        dist tmp = top(); int u = tmp.num; pop(); 
        if(vis[u]) continue; vis[u] = 1; 
        for(int i = A.head[u]; i; i = A.e[i].next)
        {
            int v = A.e[i].to; 
            if(dis1[v] > tmp.dis + A.e[i].cost && !vis[v])
            {
                dis1[v] = tmp.dis + A.e[i].cost; 
                if(!in[v]) push((dist) { v, max(dis1[v], dis2[v]) }); 
            }
        }
        for(int i = B.head[u]; i; i = B.e[i].next)
        {
            int v = B.e[i].to; in[v]--; 
            dis2[v] = max(dis2[v], tmp.dis); 
            if(!in[v]) push((dist) { v, max(dis1[v], dis2[v]) }); 
        }
    }
}

int main()
{
    n = read(); m = read();
    for(int i = 1; i <= m; i++)
    {
        int u = read(), v = read(), w = reaD();
        A.adde(u, v, w); 
    }
    for(int i = 1; i <= n; i++)
    {
        in[i] = reaD();
        for(int j = 1; j <= in[i]; j++)
        {
            int x = read();
            B.adde(x, i, 0); 
        }
    }
    dijkstra(); 
    printf("%d\n", max(dis1[n], dis2[n])); 
    return 0;
}

Guess you like

Origin www.cnblogs.com/ztlztl/p/11184529.html