zoukankan      html  css  js  c++  java
  • Invade the Mars dijsktra()&&优先队列

    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.
     **************************************************************************************************************************
    dijskstra()&&优先队列
    ***************************************************************************************************************************
      1 /*
      2   用优先队列,每一次把度数为0的点和最短路径的点计入队列
      3   最终还是用dijkstra()
      4 */
      5 #include<iostream>
      6 #include<cstdio>
      7 #include<cstring>
      8 #include<string>
      9 #include<algorithm>
     10 #include<map>
     11 #include<queue>
     12 #include<stack>
     13 #include<cmath>
     14 #include<vector>
     15 #define inf 0x3f3f3f3f
     16 #define Inf 0x3FFFFFFFFFFFFFFFLL
     17 #define eps 1e-9
     18 #define pi acos(-1.0)
     19 using namespace std;
     20 typedef long long ll;
     21 const int maxn=3000+10;
     22 const int maxm=70000+10;
     23 struct Edge
     24 {
     25     int v,w,next;
     26 };
     27 struct HeapNode
     28 {
     29     int d,u;
     30     bool operator < (const HeapNode &a) const
     31     {
     32         return d>a.d;
     33     }
     34 };
     35 Edge edges[maxm<<1];
     36 int head[maxn],pro[maxn],nEdge,n,m;
     37 bool vis[maxn];
     38 ll d[maxn],dt[maxn];
     39 vector<int>lim[maxn];
     40 void AddEdge(int u,int v,int w)
     41 {
     42     nEdge++;
     43     edges[nEdge].v=v;
     44     edges[nEdge].w=w;
     45     edges[nEdge].next=head[u];
     46     head[u]=nEdge;
     47 }
     48 void Init()
     49 {
     50     for(int i=0;i<=n;++i) lim[i].clear();
     51     memset(head,0xff,sizeof(head));
     52     memset(dt,0,sizeof(dt));
     53     memset(pro,0,sizeof(pro));
     54     memset(vis,0,sizeof(vis));
     55     nEdge=-1;
     56 }
     57 void dijkstra()
     58 {
     59     for(int i=1;i<=n;++i) d[i]=Inf;
     60     d[1]=0;
     61     priority_queue<HeapNode>q;
     62     HeapNode hp;
     63     hp.d=0;hp.u=1;
     64     q.push(hp);
     65     int u,v,sz;
     66     while(!q.empty())
     67     {
     68         hp=q.top();q.pop();
     69         u=hp.u;
     70         if(vis[u]) continue;
     71         vis[u]=true;
     72         sz=lim[u].size();
     73         for(int i=0;i<sz;++i)
     74         {
     75             v=lim[u][i];
     76             pro[v]--;
     77             dt[v]=max(dt[v],d[u]);
     78             if(pro[v]==0&&d[v]!=Inf)
     79             {
     80                 d[v]=max(dt[v],d[v]);
     81                 hp.u=v;hp.d=d[v];
     82                 q.push(hp);
     83             }
     84         }
     85         for(int k=head[u];k!=-1;k=edges[k].next)
     86         {
     87             v=edges[k].v;
     88             if(d[v]>d[u]+edges[k].w)
     89             {
     90                 d[v]=max(d[u]+edges[k].w,dt[v]);
     91                 hp.u=v;hp.d=d[v];
     92                 if(pro[v]==0) q.push(hp);
     93             }
     94         }
     95     }
     96 }
     97 int main()
     98 {
     99     int t;
    100     scanf("%d",&t);
    101     while(t--)
    102     {
    103         scanf("%d%d",&n,&m);
    104         Init();
    105         int u,v,w;
    106         for(int i=0;i<m;++i)
    107         {
    108             scanf("%d%d%d",&u,&v,&w);
    109             AddEdge(u,v,w);
    110         }
    111         for(int i=1;i<=n;++i)
    112         {
    113             scanf("%d",&u);
    114             pro[i]=u;
    115             while(u--)
    116             {
    117                 scanf("%d",&v);
    118                 lim[v].push_back(i);
    119             }
    120         }
    121         dijkstra();
    122         printf("%I64d
    ",d[n]);
    123     }
    124     return 0;
    125 }
    View Code
  • 相关阅读:
    RedisCacheTool参考其中的文件读写功能
    eclipse eayExplorer 查看代码的资源管理器打开方式
    有关写代码效率的问题
    Eclipse
    解决pdm打开只显示表名不显示字段的步骤
    绝对定位元素的水平垂直居中
    Maven依赖之Scope
    无提示关闭弹出窗口
    Maven模块与模块间的依赖
    Maven+Hibernate4注解0配置示例
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3432315.html
Copyright © 2011-2022 走看看