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函数。

附上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define M 70005
#define N 3005 typedef pair <long long, int > pii; struct Edge
{
int from,to,val;
int next;
} edge[M]; int n,m,tol;
int head[M],pt[N];
long long dis[N],dn[N]; ///dis记录行走的距离时间,dn记录解除保护的时间
bool vis[N];
vector<int> p[N]; ///容器 int max(int a,int b)
{
return a>b?a:b;
} void addEdge(int u,int v,int val)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].val=val;
edge[tol].next=head[u];
head[u]=tol++;
} void spfa(int s)
{
memset(dis,INF,sizeof(dis));
memset(vis,false,sizeof(vis));
priority_queue<pii,vector<pii>,greater<pii> > q;
while(!q.empty()) q.pop();
dis[s]=;
q.push(make_pair(dis[s],s));
while(!q.empty())
{
pii u=q.top();
q.pop();
int x=u.second;
if(vis[x]) continue;
vis[x]=true;
int y=p[x].size();
int k=;
while(k<y) ///被保护的城市
{
int z=p[x][k];
pt[z]--;
dn[z]=max(dn[z],dis[x]);
if(dn[z]!=INF&&pt[z]==)
{
dis[z]=max(dis[z],dn[z]);
q.push(make_pair(dis[z],z));
}
k++;
}
for(int i=head[x]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[x]+edge[i].val)
{
dis[v]=max(dis[x]+edge[i].val,dn[v]);
if(!pt[v]) q.push(make_pair(dis[v],v)); }
}
}
} void init()
{
tol=;
memset(head,-,sizeof(head));
memset(dn,,sizeof(dn));
for(int i=; i<=n; i++)
p[i].clear();
} int main()
{
int T,i,j,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
}
for(i=; i<=n; i++)
{
scanf("%d",&k);
pt[i]=k;
while(k--)
{
scanf("%d",&j);
p[j].push_back(i);
}
}
spfa();
printf("%I64d\n",dis[n]);
}
return ;
}

hdu 3873 Invade the Mars(有限制的最短路 spfa+容器)的更多相关文章

  1. HDU 3873 Invade the Mars(带限制条件的Dijkstra)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3873 思路: 军队可以先等待在城市外面,等保护该城市的城市都被攻破后,直接进城(即进城不用耗费时间). ...

  2. hdu3873 Invade the Mars 有限制的最短路

    此段略过.看完题目,觉得这真的是一道好题目.自己有想法,但是实现起来却很难.看题解,写代码,然后写题解,意义何在?我不认为自己总是这么弱.就算抄代码,我也要有自己的理解.菜鸟总会成长. 首先,题目必须 ...

  3. hdu 3790 最短路径问题(两个限制条件的最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=3790 有两个条件:距离和花费.首先要求距离最短,距离相等的条件下花费最小. dijkstra,仅仅是在推断条件时 ...

  4. hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路

    Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...

  5. HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M ...

  6. POJ 3835 &amp; HDU 3268 Columbus’s bargain(最短路 Spfa)

    题目链接: POJ:http://poj.org/problem?id=3835 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3268 Problem ...

  7. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

  8. HDU - 3035 War(对偶图求最小割+最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3035 题意 给个图,求把s和t分开的最小割. 分析 实际顶点和边非常多,不能用最大流来求解.这道题要用 ...

  9. HDU 5876 Sparse Graph(补图中求最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...

随机推荐

  1. python实例 文件处理

    对比Java,python的文本处理再次让人感动 #! /usr/bin/python spath="D:/download/baa.txt" f=open(spath," ...

  2. VI/VIM编辑器快捷键

    常用快捷键: Ctrl+f        向下翻页 Ctrl+b       向上翻页 G                移动到文件最后一行 gg              移动到文件第一行 N+回车 ...

  3. 快速乘O(1)和O(log)

    O(1)快速乘来自骆可强:<论程序底层优化的一些方法与技巧> //O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MO ...

  4. 洛谷P1368 均分纸牌(加强版) [2017年6月计划 数论14]

    P1368 均分纸牌(加强版) 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,纸牌总数必为 N 的倍数.可以在任一堆上取1张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取 ...

  5. redis书籍

    redis中文官网命令网址:http://doc.redisfans.com/ redis英文官网命令网址:https://redis.io/commands redis书籍 由 Karl Segui ...

  6. jquery Select2 学习笔记之中文提示 - CSDN博客

    首先学习这个东西呢,还是看官网比较全面 select2官网例子 要select2中文显示:必须要引入中文包,且一定要放在select2.js之后 [javascript] view plain cop ...

  7. Android的ADB学习笔记

    1.ADB的常用命令   Pull命令:adb -e|-d pull {文件的路径} {获取文件路径} 2. 文件操作的基本命令 ls -al:显示当下目录下用户对文件的操作权限.  = la -al ...

  8. Django项目:CRM(客户关系管理系统)--08--03PerfectCRM创建基本数据03

    如果感觉本章博客对您有帮助,请尽情打赏吧!

  9. wamp httpd-vhosts.conf

    配置Apache的httpd.conf文件 Include conf/extra/httpd-vhosts.conf 修改apache的vhost文件 <VirtualHost *:> D ...

  10. Win7系统中wmiprvse.exe占用CPU高如何解决

    该进程的详细路径是在:C:\WINDOWS\System32\Wbem  我们可以在任务管理器中“wmiprvse.exe”进程上单击右键,选择“打开文件位置”即可看到,如果该文件不在该文件夹中,那么 ...