Invade the Mars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 365768/165536 K (Java/Others)Total Submission(s): 1464 Accepted Submission(s): 408
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.
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 3001;
const int INF = 0x7fffffff;
typedef struct{
int to, cost;
}node;
node temp;
vector<node>edge[maxn];
vector<int>protect[maxn];
bool vis[maxn];
int in[maxn];
int dist[maxn];//記錄每個城市被攻佔的時間
int pre[maxn]; //記錄保護該城市的的城市被攻佔的最晚時間
void dijkstra(int& n)
{
for (int i = 1; i <= n; i ++) {
pre[i] = 0, dist[i] = INF, vis[i] = false;
}
dist[1] = 0;
for (int i = 1; i <= n-1; i ++) {
int minc = INF, pos = -1;
for (int j = 1; j <= n; j ++) {
if (in[j] == 0 && vis[j] == false && dist[j] < minc) {
minc = dist[j], pos = j;
}
}
if (pos == -1) return;
vis[pos] = true;
if (protect[pos].size() != 0) {
for (int j = 0; j < (int)protect[pos].size(); j ++) {
pre[protect[pos][j]] = max(pre[protect[pos][j]], dist[pos]);
in[protect[pos][j]] --;
}
}
int to;
for (int j = 0; j < (int)edge[pos].size(); j ++) {
to = edge[pos][j].to;
if (vis[to] == false) {
if (dist[to] > dist[pos]+edge[pos][j].cost) {
dist[to] = dist[pos]+edge[pos][j].cost;
}
}
}
for (int j = 1; j <= n; j ++) { //在這個地方更新dist,dist是pre和dist中的最大值。
dist[j] = max(dist[j], pre[j]);
}
}
}
int main() {
int t, n, m, x, y;
scanf("%d", &t);
while (t --) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++) {
edge[i].clear(), in[i] = 0, protect[i].clear();
}
for (int i = 0; i < m; i ++) {
scanf("%d%d%d", &x, &temp.to, &temp.cost);
edge[x].push_back(temp);
}
for (int i = 1; i <= n; i ++) {
scanf("%d", &x);
for (int j = 0; j < x; j ++) {
scanf("%d", &y);
protect[y].push_back(i);
in[i] ++;
}
}
dijkstra(n);
printf("%d\n", dist[n]);
}
return 0;
}
/*
2
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
6 6
1 2 1
2 3 1
3 4 1
4 6 1
1 5 100
5 4 100
0
0
0
1 5
0
0
*/