2011多校 dijkstra最短路变形

 hdu Problem 3873 Invade the Mars

对算法的理解还是不到位, 导致卡了一天!

而且有个小地方, 就是dist是inf时, des有值的时候,时间不能取成des, 这个错误找了好久。。

优先队列入队的时候并不是最优的解, 出队时满足dist【cur.u】>=cur.w才是最优解, 因此更新被限制节点时是要在出队时更新。

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
typedef long long ll;
const int maxn=3000+123;
const int maxm=70000+123;
const int inf=0x3f3f3f3f;
const long long infl=200000000000000000ll;

struct Edge{
    int v, next;
    ll w;
}edge[maxm];
int head[maxn], cnt;
void addedge(int u, int v, ll w)
{
    edge[cnt].v=v; edge[cnt].w=w;
    edge[cnt].next=head[u]; head[u]=cnt++;
}

struct Node{
    int u;
    ll w;
    bool operator<(Node a)const{
        return w>a.w;
    }
};
vector<int> product[maxn];
int deg[maxn];

ll getmax(ll a, ll b)
{
//    if(a==infl)return b;
//    if(b==infl)return a;
    return max(a, b);
}

long long dist[maxn]; long long des[maxn];
void Dijkstra(int s, int n)
{
    for (int i=0; i<n; ++i)dist[i]=infl;
    memset (des, 0, sizeof(des));
    dist[s]=0; 
    priority_queue<Node> Q;
    queue<Node> Qtmp;
    Node cur;
    cur.u=s; cur.w=0;
    Q.push(cur);
    while (!Q.empty())
    {
        cur=Q.top();
        Q.pop();
        //printf("cur === %d  dist==%I64d  w==%I64d   deg=%d\n", cur.u, getmax(dist[cur.u], des[cur.u]), cur.w, deg[cur.u]);
        if(getmax(dist[cur.u], des[cur.u])<cur.w)continue;
        
        int sz=product[cur.u].size();
        for (int i=0; i<sz; ++i)
        {
            const int &vex=product[cur.u][i];
            if(deg[vex]>0)
            {
                deg[vex]--;
                des[vex]=getmax(des[vex], getmax(dist[cur.u], des[cur.u]));
                if(deg[vex]==0)
                {
                    Node tmp;
                    tmp.u=vex;
                    tmp.w=getmax(des[vex], dist[vex]);
                    Q.push(tmp);
                }
            }
        }
        
        for (int p=head[cur.u]; ~p; p=edge[p].next)
        {
            //printf("v=%d deg=%d dist=%I64d des=%I64d\n", edge[p].v, deg[edge[p].v], dist[edge[p].v], des[edge[p].v]);
            if(dist[edge[p].v]>getmax(dist[cur.u], des[cur.u])+edge[p].w)
            {
                dist[edge[p].v]=getmax(dist[cur.u], des[cur.u])+edge[p].w;
                if(deg[edge[p].v]==0)
                {
                    Node tmp;
                    tmp.u=edge[p].v;
                    tmp.w=getmax(dist[edge[p].v], des[edge[p].v]);
                    Q.push(tmp);
                }
            }
        }
        
    }
}
void init()
{
    memset (head, -1, sizeof(head));
    cnt=0;
}


int main()
{
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int cas; scanf("%d", &cas);
    while (cas--)
    {
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i=0; i<m; ++i)
        {
            int a, b, c; scanf("%d%d%d", &a, &b, &c);
            a--, b--;
            addedge(a, b, (long long)c);
        }
        for (int i=0; i<n; ++i)
            product[i].clear();
        for (int i=0; i<n; ++i)
        {
            scanf("%d", deg+i);
            des[i]=deg[i]?infl:0;
            for (int j=0; j<deg[i]; ++j)
            {
                int x; scanf("%d", &x);
                product[x-1].push_back(i);
            }
        }
        Dijkstra(0, n);
//        for (int i=0; i<n; ++i)printf("%I64d  %I64d\n", dist[i], des[i]);
        printf("%I64d\n", getmax(dist[n-1], des[n-1]));
    }
    return 0;
}
/*
10
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
1 4 3
2 3 4
2 5 2
4 6 2
5 3 2
0
0
0
1 3
0
2 3 5

6 6
1 2 1
1 4 3
2 3 6
2 5 10
4 6 2
5 3 2
0
0
0
1 3
0
2 3 5
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值