zoukankan      html  css  js  c++  java
  • hdu 3873 Invade the Mars(有限制的最短路 spfa+容器)

    Invade the Mars

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 365768/165536 K (Java/Others)
    Total Submission(s): 2079    Accepted Submission(s): 612


    Problem Description
    It's now the year 21XX,when the earth will explode soon.The evil U.S. decided to invade the Mars to save their lives.
    But the childlike Marsmen never keeps any army,because war never take place on the Mars.So it's very convenient for the U.S. to act the action.
    Luckily,the Marsmen find out the evil plan before the invadation,so they formed a defense system.The system provides enchantment for some citys,and the enchantment generator for city A maybe set in city B,and to make things worse,both city B and C and more will provide echantment for city A.
    The satelite of U.S. has got the map of the Mars.And they knows that when they enter a city,they can destory all echantment generator in this city at once,and they can enter a city only if they has destoryed all enchantment generator for this city,but troops can stay at the outside of the city and can enter it at the moment its echantment is destoryed.Of course the U.S. army will face no resistance because the Mars keep no army,so troops can invade in many way at the same time.
    Now the U.S. will invade the Mars,give you the map,your task is to calculate the minimium time to enter the capital of the Mars.
     
    Input
    The first line contains an integer T,which is the number of test cases.
    For each testcase:
    The first line contains two integers N and M,1<=N<=3000,1<=M<=70000,the cities is numbered from 1 to N and the U.S. landed on city 1 while the capital of the Mars is city N.
    The next M lines describes M paths on the Mars.Each line contains three integers ai,bi and wi,indicates there is a unidirectional path form ai to bi lasts wi minutes(1<=wi<=10^8).
    The next N lines describes N citys,the 1+M+i line starts with a integer li,followed with li integers, which is the number of cities has a echantment generator protects city i.
    It's guaranteed that the city N will be always reachable.
     
    Output
    For each case,print a line with a number indicating the minimium time needed to enter the capital of the Mars.
     
    Sample Input
    1
    6 6
    1 2 1
    1 4 3
    2 3 1
    2 5 2
    4 6 2
    5 3 2
    0
    0
    0
    1 3
    0
    2 3 5
     
    Sample Output
    5
    Hint
    The Map is like this: We can follow these ways to achieve the fastest speed: 1->2->3,1->2->5,1->4->6。
    题意:

    进攻n个城市,有些城市可能受其他城市保护,

    如果i城市受j城市保护,那么你必须先攻占j城市才能再攻占i城市,问你攻占城市n的最短时间是多少。

    数据解释:

    给定t, 表示有t组数据

    给定n,m 表示n个点,m条边

    接下来m条有向边, a,b,c  表示从a到b,距离为c

    接下来n行, 每行第一个整数d,然后接下来是d个整数,x1,x2,...xd, 表示第i个城市受d个城市保护,表示只有城市x1,x2...xd都被攻占,城市i才能被攻占

    问从点1到达点n的最短时间(一定是可达的)

    重要的一点是,因为是军队,所以可以同时进军多个点。

    参照网上大神的思路:

    用一个数组pt记录某城市被保护的次数(城墙的耐久度)。当pt[i]==0时,并且该城市没有被占领,那么军队占领该城市的时间dist[i]=max(dist[i], dn[i]),其中dn[i]表示该城市脱离保护的时间(也就是该城市可能被占领的最小时间)。此时,如果j城市(未被占领)与i城市相邻,则dist[j]=dist[i] + w, w是e[i][j]的边权。这样就有一个大致的模型了。还要注意,在确定i城市被占领的时间的时候,要判断军队是否能到达i城市。也就是判断dist[i]!=INF?

      为了避免dist[]不断松弛,需要用优先队列。每次弹出来的点能保证是到达该点的最短距离。当某点不收保护时,就入队。最短距离如果已经确定,就标记,不用再去更新距离(其实无法更新,因为已经是最短距离)。

      在知道i点的最短距离后,去计算相邻的j点的最短距离时。需要dist[i]和i。因为是按照最短距离的大小而弹出队列的,dist[i]需要入队。占领i城市以后,它所保护的城市将不再收到它的保护。需要枚举。所以需要将i入队。然后就用了pair函数。

    附上代码:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <queue>
      5 #include <vector>
      6 #include <algorithm>
      7 using namespace std;
      8 #define INF 0x3f3f3f3f
      9 #define M 70005
     10 #define N 3005
     11 
     12 typedef pair <long long, int > pii;
     13 
     14 struct Edge
     15 {
     16     int from,to,val;
     17     int next;
     18 } edge[M];
     19 
     20 int n,m,tol;
     21 int head[M],pt[N];
     22 long long dis[N],dn[N]; ///dis记录行走的距离时间,dn记录解除保护的时间
     23 bool vis[N];
     24 vector<int> p[N]; ///容器
     25 
     26 int max(int a,int b)
     27 {
     28     return a>b?a:b;
     29 }
     30 
     31 void addEdge(int u,int v,int val)
     32 {
     33     edge[tol].from=u;
     34     edge[tol].to=v;
     35     edge[tol].val=val;
     36     edge[tol].next=head[u];
     37     head[u]=tol++;
     38 }
     39 
     40 void spfa(int s)
     41 {
     42     memset(dis,INF,sizeof(dis));
     43     memset(vis,false,sizeof(vis));
     44     priority_queue<pii,vector<pii>,greater<pii> > q;
     45     while(!q.empty()) q.pop();
     46     dis[s]=0;
     47     q.push(make_pair(dis[s],s));
     48     while(!q.empty())
     49     {
     50         pii u=q.top();
     51         q.pop();
     52         int x=u.second;
     53         if(vis[x]) continue;
     54         vis[x]=true;
     55         int y=p[x].size();
     56         int k=0;
     57         while(k<y)  ///被保护的城市
     58         {
     59             int z=p[x][k];
     60             pt[z]--;
     61             dn[z]=max(dn[z],dis[x]); 
     62             if(dn[z]!=INF&&pt[z]==0)
     63             {
     64                 dis[z]=max(dis[z],dn[z]);
     65                 q.push(make_pair(dis[z],z));
     66             }
     67             k++;
     68         }
     69         for(int i=head[x]; i!=-1; i=edge[i].next)
     70         {
     71             int v=edge[i].to;
     72             if(dis[v]>dis[x]+edge[i].val)
     73             {
     74                 dis[v]=max(dis[x]+edge[i].val,dn[v]);
     75                 if(!pt[v]) q.push(make_pair(dis[v],v));
     76 
     77             }
     78         }
     79     }
     80 }
     81 
     82 void init()
     83 {
     84     tol=0;
     85     memset(head,-1,sizeof(head));
     86     memset(dn,0,sizeof(dn));
     87     for(int i=1; i<=n; i++)
     88         p[i].clear();
     89 }
     90 
     91 int main()
     92 {
     93     int T,i,j,k;
     94     scanf("%d",&T);
     95     while(T--)
     96     {
     97         scanf("%d%d",&n,&m);
     98         init();
     99         int a,b,c;
    100         while(m--)
    101         {
    102             scanf("%d%d%d",&a,&b,&c);
    103             addEdge(a,b,c);
    104         }
    105         for(i=1; i<=n; i++)
    106         {
    107             scanf("%d",&k);
    108             pt[i]=k;
    109             while(k--)
    110             {
    111                 scanf("%d",&j);
    112                 p[j].push_back(i);
    113             }
    114         }
    115         spfa(1);
    116         printf("%I64d
    ",dis[n]);
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    随笔2
    随笔
    关于updateElement接口
    随笔1
    本地访问正常,服务器访问乱码 记录
    Redis (error) NOAUTH Authentication required.解决方法
    tomcat启动很慢 停留在 At least one JAR was scanned for TLDs yet contained no TLDs.
    微信公众号消息回复
    微信公众号 报token验证失败
    idea中web.xml报错 Servlet should have a mapping
  • 原文地址:https://www.cnblogs.com/pshw/p/5768240.html
Copyright © 2011-2022 走看看