zoukankan      html  css  js  c++  java
  • [题解] [SDOI2010] 大陆争霸

    题面

    题解

    对于到某个点(i), 我们有两个条件

    到达(i)点的最短时间, 用(dis1_i)表示

    破坏完所有保护(i)点的城市的最小时间

    两者取(max)即到(i)点的最小时间

    对于破坏某个城市的保护点, 用类似于拓扑序的方式处理即可

    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
    ", max(dis1[n], dis2[n])); 
    	return 0;
    }
    
  • 相关阅读:
    Unity给力插件之MegaFiers
    序列化存档之备忘脚本
    09 Spring Cloud的集群保护框架Hystrix
    08 在Spring Cloud中使用Feign
    07 REST客户端
    06 RestTemplate负载均衡
    05 第一个Ribbon程序
    04 Ribbon介绍
    01 在IDEA的同一目录下创建多个项目
    03 Eureka集群的搭建
  • 原文地址:https://www.cnblogs.com/ztlztl/p/11184529.html
Copyright © 2011-2022 走看看