hdu 3873 Invade the Mars 有保护节点的最短路

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

//

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
//可以先占领,只有当节点的所有被保护节点被摧毁后才可以摧毁
const long long inf=((long long)1<<60);
const int maxn=3100;
const int maxm=71000;
struct Edge
{
    int t,w;
    int next;
};
int n,m;
int p[maxn];
Edge G[maxm];
int l;
void init()
{
    memset(p,-1,sizeof(p));
    l=0;
}
void addedge(int u,int t,int w)
{
    G[l].w=w;
    G[l].t=t;
    G[l].next=p[u];
    p[u]=l++;
}
vector<int> prot[maxn];//i被保护的节点
int in[maxn];//i被in[i]个节点保护
long long les[maxn];//所有保护i的节点被摧毁的最早时间中的最晚时间
long long d[maxn];//节点i被摧毁的最早时间
int vis[maxn];
typedef pair<long long,int> Node;
void dijkstra(int s)
{
    memset(vis,0,sizeof(vis));
    memset(les,0,sizeof(les));
    for(int i=1;i<=n;i++) d[i]=inf;
    d[s]=0;
    priority_queue<Node,vector<Node>,greater<Node> > q;//队列中的节点都是没有被保护节点的
    q.push(make_pair(d[s],s));
    while(!q.empty())
    {
        Node cnt=q.top();q.pop();
        int x=cnt.second;
        if(vis[x]) continue;
        vis[x]=1;
        //更新x保护的节点
        for(int j=0;j<prot[x].size();j++)
        {
            int t=prot[x][j];//x保护j
            in[t]--;//x处理掉,j的保护度-1
            les[t]=max(les[t],d[x]);
            if(in[t]==0)//t脱离保护
            {
                //如果d[t]==inf 则d[t]肯定大于les[t],且d[t]在后面更新;
                //如果d[t]!=inf 则d[t]=max(d[t],les[t]);
                d[t]=max(d[t],les[t]);
                if(d[t]!=inf)
                {
                    q.push(make_pair(d[t],t));
                }
            }
        }
        //更新d[i]
        for(int i=p[x];i!=-1;i=G[i].next)
        {
            int t=G[i].t,w=G[i].w;
            if(d[t]>d[x]+w)
            {
                d[t]=max(d[x]+w,les[t]);
                if(in[t]==0)
                {
                   q.push(make_pair(d[t],t));
                }
            }
        }
    }
}
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            int u,t,w;scanf("%d%d%d",&u,&t,&w);
            addedge(u,t,w);
        }
        for(int i=1;i<=n;i++) prot[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&in[i]);//被保护的点的个数
            for(int j=0;j<in[i];j++)
            {
                int x;scanf("%d",&x);
                prot[x].push_back(i);//x保护i
            }
        }
        dijkstra(1);
        printf("%I64d\n",d[n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值