hdu Invade the Mars(侵入火星)

Invade the Mars

Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 365768/165536K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

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.

Source

2011 Multi-University Training Contest 4 - Host by SDU
唉~~~~菜  这个题自己没做出,看了别人题解才做出的。
题意:n个点m条边,求从1入侵到n点所需的最短时间,因为军队是同时开始入侵的,有些地方不能直接入侵,必须先入侵其他点,才能入侵他。
但是军队可以在这个点的旁边等,等待友军将其他点入侵,则可以直接入侵那个点。其余的就可以直接入侵
第m+1行表示的是入侵这个点前必须先入侵那些点,第一个数是 个数,之后就是要先入侵的点
思路:dij斯特拉算法,边求1到其他点的最短路,边入侵点,当这个点被入侵成功,就看看有没有其他点是先要入侵这个点才能入侵他的。如果有则
将那个点的必须先入侵其他点的个数减1,当减到0的时候就表示那个点也可以入侵了, 则他们可以进入队列,来更新其他点了,到这个点的最短路等于
MAX(这个点的最短路,入侵这个点所要花费的时间)。大家记得因为军队是同时出发的,所以入侵这个点的所要花费的时间,就是入侵这个点前先要
入侵其他点的最大的最短路。就是这里 想了好久,当时没理解题意 是军队同时出发的,我当时想的是一个人出发。
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stdlib.h>
#include<vector>
using namespace std;
#define INF 9e12
struct edge
{
    int from,to;
    long long int w;
};
struct node
{
    int x;
    int d;
     friend bool operator < (node n1,node n2)
    {
        return n1.d>n2.d; //<为从大到小,>大于从小到大;
    }
};
vector<int>G[3010];
vector<edge>edges;
vector<int>baohu[3010];
int  in[3010];
long long int dis[3010];//最短路
long long int gz[3010];//入侵事件
int n,m;
void addedge(int x,int y,long long int w)
{
    edge a={x,y,w};
    edges.push_back(a);
    G[x].push_back(edges.size()-1);
}
long long int max1(long long int x,long long int y)
{
    if(x<y) return y;
    return x;
}
void SPFA()
{
    for(int i=0;i<=n;i++)
    {
        dis[i]=INF;
        gz[i]=0;
    }
    priority_queue<node>q;//要用优先队列,不然会超时

    dis[1]=0;
    int vis[3010];
    memset(vis,0,sizeof(vis));
    node a={1,0},b;
    q.push(a);
    while(!q.empty())
    {
       a=q.top();
       q.pop();
if(!vis[a.x])//只能有一次去判断这个点保护了其他点,所以要标记,不然会出错。
    {
    vis[a.x]=1;

      for(int i=0;i<baohu[a.x].size();i++)//a.x点被入侵,看看有没有其他点是先要入侵a.x这个点的
           {
           int v=baohu[a.x][i];
           in[v]--;
           gz[v]=max1(gz[v],dis[a.x]);
           if(in[v]==0&&dis[v]!=INF)
           {
             dis[v]=max1(dis[v],gz[v]);
             node b={v,dis[v]};
             q.push(b);
           }
         }
    }
        for(int i=0;i<G[a.x].size();i++)
           {
               edge v=edges[G[a.x][i]];
               if(dis[a.x]+v.w<dis[v.to])
               {
                   dis[v.to]=max1(dis[a.x]+v.w,gz[v.to]);//求得最短路
                   if(in[v.to]==0)//如果这个点已经入侵完毕了,则可以将它放入队列,更新其他点
                   {   node b={v.to,dis[v.to]};
                       q.push(b);
                   }
               }
           }

    }
    printf("%lld\n",dis[n]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            G[i].clear();
            baohu[i].clear();
        }
        edges.clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;
            long long int w;
            scanf("%d %d %lld",&x,&y,&w);
            addedge(x,y,w);
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&in[i]);
            for(int j=0;j<in[i];j++)
            {
                int a;
                scanf("%d",&a);
                baohu[a].push_back(i);//
            }
        }
        SPFA();

    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值