Continental hegemony [SDOI2010] with restrictions shortest

As long as you have an infinite number of robot blew, you will be able to do whatever they want Springfield · Brotherton

Description [title]
slightly
Jason state-owned \ (N \) cities, by the \ (M \) connected unidirectional path. Jason is the capital city of the country \ (N \) . You only need to destroy the country's capital Jason will be able to win. In order to minimize the consumption of one's own, you decide to use the robot blew accomplish this task. The only difficulty is that part of the city enchantment Jason countries have to protect, not destroy the enchantment will not be able to enter the city. And each city ward is maintained by the distribution in other cities some enchantment generator, if you want to enter a city, you have to destroy all the enchantment generator to maintain the enchantment of this city. Now you have an infinite number of robot blew, a city once entered, the robot can instantly blew detonated, destroying a target (enchantment generator, or Jason capital of the country), and of course the robot itself will be destroyed together. You need to know: beat Jason minimum time required for the country.
[Input format
of the first row two positive integers N, M. Next M lines, each line three positive integers \ (u_i, v_i, w_i \) , expressed from a city \ (u_i \) to the city \ (v_i \) one-way road, blew through this path requires robots \ (w_i \) time. After \ (N \) lines, each describing a city. The first is a positive integer \ (L_i \) , the number of representatives to maintain this Enchantment Enchantment city generator used. After \ (L_i \) a \ (. 1 \) ~ \ (N \)City ID, that represents the position of each enchantment generator. If \ (L_i = 0 \) , it indicates that the city is no enchantment protection, ensure \ (L_1 = 0 \) .
[Output format]
contains only a positive integer, beat Jason minimum time required for the country.
---
[coaching] idea
if there is no limit enchantment, it is clear that this problem will just have to run it again \ (Dijkstra \) before.
At first glance this question thought topological sorting.
Was written in the examination room before a shortest distance to each point shortest calculated source point, and then update the topology Ward sequence generator \ (ans [i] = max (dist [i], ans [j]) \) , which city \ (j \) enchantment generator to maintain the city \ (i \) enchantment.
Large sample once, flattered
the end of the exam,
9a2aabb07941471c96828e92da2efb90.jpeg
\ (\ Color Red {} {Wrong \ Answer \ qquad10} \)
But in fact, this problem will only need minor changes on the shortest path algorithm on the line.
Open three arrays \ (dist, TME, ANS \) , \ (dist [I] \) represents a source point \ (I \) is the shortest distance, \ (TME [I] \) represents the point\ (i \) was first released at this time enchantment, \ (ans [i] \) indicates that the city \ (i \) was first able to \ (ans [i] \) blown upon.
Obviously, \ (ANS [I] = max (dist [I], TME [I]) \) .
For \ (TME [I] \) , \ (\ FORALL J \ in L [I], TME [I] = max (ANS [J]) \) , where \ (L [i] \) Representative maintained City \ (i \) collection enchantment of the city.
Then directly run again \ (Dijkstra \) , but you want to have updated \ (L [i] \) after all of the city in order to \ (i \) into the heap.
The final answer is \ (ANS [n-] \) , the time complexity \ (O (log n-m) \) .

【Code】

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define re register
using namespace std;
typedef long long ll;

ll n, m;
ll head[100005], pre[150005], to[150005], val[150005], len;
ll h2[100005], p2[100005], t2[100005], l2, in[100005], ans[100005], dis[100005], tme[100005];
bool vis[100005];

ll read() {
    ll ret = 0, flag = 1;
    char ch = getchar();
    while (ch > '9' || ch < '0') {
        if (ch == '-') flag = -1;
        ch = getchar(); 
    }
    while (ch <= '9' && ch >= '0') {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * flag;
}

void insert(ll u, ll v, ll w) {
    to[++len] = v, val[len] = w, pre[len] = head[u], head[u] = len;
} 

void insert2(ll u, ll v) {
    t2[++l2] = v, p2[l2] = h2[u], h2[u] = l2;
} 

void dijkstra() {
    for (re int i = 1; i <= n; i++) dis[i] = 0x7fffffff;
    dis[1] = tme[1] = ans[1] = 0;
    priority_queue< pair<ll, ll> , vector< pair<ll, ll> > , greater< pair<ll, ll> > > q;
    q.push(make_pair(0, 1));
    while (!q.empty()) {
        ll c = q.top().second;
        q.pop();
        if (vis[c]) continue;
        vis[c] = 1;
        for (re ll i = head[c]; i != 0; i = pre[i]) {
            if (ans[c] + val[i] < dis[to[i]]) {
                dis[to[i]] = ans[c] + val[i];   
                if (!in[to[i]]) {
                    ans[to[i]] = max(dis[to[i]], tme[to[i]]);
                    q.push(make_pair(ans[to[i]], to[i]));
                }
            }
        }
        for (re ll i = h2[c]; i != 0; i = p2[i]) {
            if (in[t2[i]]) {
                in[t2[i]]--;
                tme[t2[i]] = max(tme[t2[i]], ans[c]);   
                if (!in[t2[i]]) {
                    ans[t2[i]] = max(dis[t2[i]], tme[t2[i]]);
                    q.push(make_pair(ans[t2[i]], t2[i]));
                }
            }
        }
    }
}

int main() {
    n = read();
    m = read();
    ll u, v, w;
    for (re int i = 1; i <= m; i++) {
        u = read(), v = read(), w = read();
        insert(u, v, w);
    }
    for (re int i = 1; i <= n; i++) {
        in[i] = read();
        for (re int j = 1; j <= in[i]; j++) {
            u = read();
            insert2(u, i); 
        }
    }
    dijkstra();
    printf("%lld\n", ans[n]); 
    return 0;
} 

Guess you like

Origin www.cnblogs.com/ak-dream/p/AK_DREAM2.html