hdu 3873 dijkstra

本文介绍了一个关于入侵火星的算法竞赛题目,目标是最小化进入火星首都的时间。涉及到城市间的单向路径及其消耗时间,以及各城市的防御机制。通过构建图模型,并采用改进的Dijkstra算法进行解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Invade the Mars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 365768/165536 K (Java/Others)
Total Submission(s): 794    Accepted Submission(s): 236


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
 

Recommend
lcy
 
做出这道题很开心呀~~完全自己想的~
思路将每一个点多加一个cou值表示其收cou个点限制,使用优先队列弹出时因为终点一定能到达,所以弹出cou最小为0,更新其相邻每个点。具体见代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#define inf 99999999
using namespace std;
int map[3005][3005];
int vis[3005];
int cou[3005];
int d[3005];
vector<int> v[3005]; 
struct node
{
    int num;
    int d;
    int cou;                         //表示其受限制点的个数
    node(int t1,int t2,int t3):num(t1),d(t2),cou(t3){}
    friend bool operator<(const node &lhs,const node &rhs)
    {
        if(lhs.cou!=rhs.cou)        //弹出最小的
            return lhs.cou>rhs.cou;
        else
            return lhs.d>rhs.d;
    }
};
int dijkstra(int n)
{
    int i;
    for(i=0;i<=n;i++)
        d[i]=inf;
    d[1]=0;
    priority_queue<node> q;
    memset(vis,0,sizeof(vis));
    q.push(node(1,0,cou[1]));
    while(!q.empty())
    {
        node temp=q.top();
        q.pop();
        if(vis[temp.num])
            continue;
        vis[temp.num]=1;
        int u=temp.num;
        vector<int>::iterator j;            //当u的最短路求出时,更新其限制的点
        for(j=v[u].begin();j!=v[u].end();j++)
        {
            cou[*j]--;
            d[*j]=max(d[*j],d[u]);
            //printf("%d %d\n",*j,d[*j]);
            q.push(node(*j,d[*j],cou[*j]));
        }
        v[i].clear();
        //printf("%d %d\n",u,d[u]);
        int i;
        for(i=1;i<=n;i++)
        {
            if(!vis[i]&&map[u][i]!=-1&&d[i]>d[u]+map[u][i])
            {

                d[i]=d[u]+map[u][i];
                q.push(node(i,d[i],cou[i]));
            }
        }
    }
    return d[n];
}
int main()
{
    int t,cas;
    scanf("%d",&t);
    for(cas=0;cas<t;cas++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(map,-1,sizeof(map));
        memset(cou,0,sizeof(cou));
        int i;
        for(i=0;i<=n;i++)
            v[i].clear();
        for(i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]==-1||map[u][v]>w)
                map[u][v]=w;
        }

        for(i=1;i<=n;i++)
        {
            int li;
            scanf("%d",&li);
            cou[i]=li;
            while(li--)
            {
                int t;
                scanf("%d",&t);
                v[t].push_back(i);    
            }
        }
        int res=dijkstra(n);
        printf("%d\n",res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值