Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为啥加dijkstra堆优化之后次短路的值是错误的?

Posted by dmcyer at 2015-04-06 19:20:06 on Problem 3463
我的代码:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#define inf 0x3fffffff
#define maxn 1005
#define maxe 1000005
using namespace std;

int first[maxn],n_ext[maxe],v[maxe],w[maxe],edge;
int st,e_nd,n,m;

struct node {
    int pos,value;
    bool pd;
    bool operator < (const node &t) const {
        return value > t.value;
    }
};
priority_queue <node> q;

inline void add(int a,int b,int c)	{
	v[++edge] = b;
	w[edge] = c;
	n_ext[edge] = first[a];
	first[a] = edge;
	return ;
}

inline int dijkstra()  {//special dijkstra
    bool vis[maxn][2];
    int d[maxn][2],cnt[maxn][2];
    node tmp,put;
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<=n;i++)d[i][0] = d[i][1] = inf;//memset(d,0x3f,sizeof(d));
    while(!q.empty())q.pop();
    d[st][0] = 0;cnt[st][0] = 1;tmp.pos = st,tmp.value = 0,tmp.pd = 0;
    q.push(tmp);
    while(!q.empty())   {
        tmp = q.top();q.pop();//printf("%d\n",d[tmp.pos][tmp.pd]);
        if(vis[tmp.pos][tmp.pd]||tmp.value>=inf)continue;
        printf("d:%d\n",tmp.value);
		vis[tmp.pos][tmp.pd] = 1;
        for(int e=first[tmp.pos];e!=-1;e=n_ext[e])   {
            if(d[v[e]][0] > d[tmp.pos][tmp.pd]+w[e])    {
				d[v[e]][1] = d[v[e]][0];//注意这里,次短路变化
				cnt[v[e]][1] = cnt[v[e]][0];
				d[v[e]][0] = d[tmp.pos][tmp.pd]+w[e];
				cnt[v[e]][0] = cnt[tmp.pos][tmp.pd];
				put.pos = v[e];
                put.value = d[v[e]][0];
                put.pd = 0;
                q.push(put);
				put.pos = v[e];
				put.value = d[v[e]][1];
				put.pd = 1;
				q.push(put);
            }
            else if(d[v[e]][0] == d[tmp.pos][tmp.pd]+w[e])
                cnt[v[e]][0] += cnt[tmp.pos][tmp.pd];//已经入队
            else if(d[v[e]][1] > d[tmp.pos][tmp.pd]+w[e])   {
                d[v[e]][1] = d[tmp.pos][tmp.pd]+w[e];
				cnt[v[e]][1] = cnt[tmp.pos][tmp.pd];
                put.pos = v[e];
                put.value = d[v[e]][1];
                put.pd = 1;
                q.push(put);
            }
            else if(d[v[e]][1] == d[tmp.pos][tmp.pd]+w[e])  
                d[v[e]][1] += cnt[tmp.pos][tmp.pd];
        }
    }
	printf("1=%d,2=%d\n",d[e_nd][0],d[e_nd][1]);
    if(d[e_nd][0]==d[e_nd][1]-1)  return cnt[e_nd][0]+cnt[e_nd][1];
    return cnt[e_nd][0];   
}

inline void init()  {
    memset(first,-1,sizeof(first));
    edge = 0;
    int x,y,z;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)   {
        scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);//add(y,x,z);
	}
    scanf("%d%d",&st,&e_nd);
    return ;
}

int main()  {
    int t;
    scanf("%d",&t);
    while(t--)  {
        init();
        printf("%d\n",dijkstra());
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator