Invade the Mars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 365768/165536 K (Java/Others)
Total Submission(s): 2695 Accepted Submission(s): 775
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
在最短路的基础上加了一个限制,必须到达其它某些点才能经过某个点,用in数组表示经过这个点必须到达其它点的数量,Max 数组表示要到达这些所有必须要到达的点的最大时间,一个点出队之后,更新Max和in数组,如果某点的in等于0,将它入队,并更新最短距离,在松弛的过程中,无论能否经过这个点,我们都将它松弛,只有in[v]等于0的情况我们才将它入队,最短距离=max(到达这个点的距离,Max[v])
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5000;
const int MAXM = 200500;
const ll INF = 1e9;
struct node1
{
int to,Next;
ll w;
}edge[MAXM];
int head[MAXN],tot;
int in[MAXN];
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
}
void addedge(int u,int v,ll w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].Next = head[u];
head[u] = tot++;
}
vector<int> p[MAXN];
ll dis[MAXN],Max[MAXN];
bool vis[MAXN];
int n;
struct node2
{
int u;
ll w;
node2(){}
node2(int _u,ll _w)
{
u = _u;
w = _w;
}
bool operator < (const struct node2& a) const
{
return w > a.w;
}
};
void Dijkstra()
{
struct node2 t;
int u,v;
for(int i = 1; i <= n; i++) {
dis[i] = INF;
vis[i] = false;
Max[i] = 0;
}
priority_queue<node2> pq;
dis[1] = 0;
pq.push(node2(1,0));
while(!pq.empty()) {
t = pq.top();
pq.pop();
u = t.u;
if(vis[u]) continue;
vis[u] = true;
for(int i = 0; i < p[u].size(); i++) {
v = p[u][i];
in[v]--;
Max[v] = max(Max[v],dis[u]);
if(dis[v] != INF && !in[v]) {
dis[v] = max(dis[v],Max[v]);
pq.push(node2(v,dis[v]));
}
}
for(int i = head[u]; i != -1; i = edge[i].Next) {
v = edge[i].to;
if(dis[v] > dis[u] + edge[i].w) {
dis[v] = max(dis[u] + edge[i].w,Max[v]);
if(!in[v]) pq.push(node2(v,dis[v]));
}
}
}
}
int main(void)
{
int T,m,u,v,num;
ll w;
scanf("%d",&T);
while(T--) {
init();
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; i++) p[i].clear();
for(int i = 1; i <= m; i++) {
scanf("%d %d %lld",&u,&v,&w);
addedge(u,v,w);
}
for(int i = 1; i <= n; i++) {
scanf("%d",&in[i]);
for(int j = 1; j <= in[i]; j++) {
scanf("%d",&v);
p[v].push_back(i);
}
}
Dijkstra();
printf("%lld\n",dis[n]);
}
return 0;
}