Invade the Mars HDU - 3873 (最短路)題解

    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.

這裏寫圖片描述

並不難的一道題,卻因爲忽略了道路是單向這一事實導致wa了好多次,以後做題一定要認真讀題了= =。
有限制的最短路,在用懶惰入隊的dij算法時,如果有一個點的保護點還沒有訪問過,即使這點時當前所有未訪問點中距離最小的,也不能入隊的,這題很多人的題解是增加一個pre數組來儲存到任意點i點所需的最短時間,然後每個點的最短路就是max(pre[i],dis[i]),但DIJ算法出隊的點的順序一定是從小到大的,所以其實只需要一個變量儲存當前的最大距離,可以在出隊時dis[i]=max(timm,dis[i]),或者入隊時dis[i]=max(timm,dis[i]),然後每次出隊後更新timm爲當前dis[i].(用dis[i]=max(timm,dis[i])之後出隊的點的距離一定是遞增的)詳見代碼。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int mod = 1000000007;
const int maxm = 140005;
const int maxn = 3005;
const int M = 25;
int n, m;
struct edge{
    int to, len, nex;
}e[maxm];
int tot;
int head[maxn];
void adde(int u, int v, int len){
    e[tot].to = v;
    e[tot].len = len;
    e[tot].nex = head[u];
    head[u] = tot++;
}
vector<int> pro[maxn];
int deg[maxn];
ll dis[maxn];
struct point{
    int u; ll dis;
    bool operator<(const point b)const{
        return dis>b.dis;
    }
};


void dij(int beg, int endd){
    ll timm = 0;
    bool vis[maxn];
    memset(vis, 0, sizeof(vis));
    priority_queue<point > que;
    que.push({ 1, 0 });
    dis[1] = 0;
    while (!que.empty()){
        point cnt = que.top(); que.pop();
        int u = cnt.u;
        if (vis[u])continue;
        dis[u] = max(dis[u], timm);
        timm = dis[u];
        if (u == endd)return;
        vis[u] = 1;
        for (int i = 0; i < pro[u].size(); i++){
            deg[pro[u][i]]--;
            if (deg[pro[u][i]] == 0){
                que.push({ pro[u][i], dis[pro[u][i]] });
            }
        }
        for (int i = head[u]; i != -1; i = e[i].nex){
            int v = e[i].to;
            if (!vis[v] && dis[u] + e[i].len < dis[v]){
                dis[v] = dis[u] + e[i].len;
                if (deg[v] == 0)
                    que.push({ v, dis[v] });
            }
        }
    }
}

int main() {
    int t, x, y, z;
    scanf("%d", &t);
    while (t--){
        scanf("%d%d", &n, &m);
        tot = 0;
        memset(head, -1, sizeof(head));
        memset(dis, 0x6f, sizeof(dis));
        for (int i = 0; i <= n; i++)pro[i].clear();
        for (int i = 0; i < m; i++){
            scanf("%d%d%d", &x, &y, &z);
            adde(x, y, z);
            //adde(y, x, z);
        }
        for (int i = 1; i <= n; i++){
            scanf("%d", &z);
            deg[i] = z;
            for (int j = 0; j < z; j++){
                scanf("%d", &x);
                pro[x].push_back(i);
            }
        }
        dij(1, n);
        printf("%lld\n", dis[n]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章