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

heap+dij why wa?

Posted by rainytry at 2010-05-04 17:11:23 on Problem 2387
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstdio>

using namespace std;

const int MAX=1001;
const int INF=2000000000;

struct point{
    int to;
    int w;
    bool operator <(const point& r) const {
        return r.w<w;
    }
};

vector < point > dot[MAX];

int dij(int start,int end)
{
    point e,ne;
    priority_queue < point > que;
    int flag[MAX],i;
    int dis[MAX];

    memset(flag,0,sizeof(flag));
    for(i=0;i<MAX;++i)
    dis[i]=INF;
    
    e.to=start;
    e.w=0;
    dis[e.to]=0;
    que.push(e);
    while(!que.empty())
    {
        e=que.top();
        que.pop();
        if(e.to==end)
        {
            return e.w;
        }
        if(flag[e.to])
        {
            continue;
        }
        flag[e.to]=1;
        for(i=0;i<dot[e.to].size();++i)
        {
            if(dot[e.to][i].w+e.w<dis[dot[e.to][i].to])
            {
                dis[dot[e.to][i].to]=dot[e.to][i].w+dis[e.to];
                ne.w=dis[dot[e.to][i].to];
                ne.to=dot[e.to][i].to;
                que.push(ne);
            }
        }
    }
}

int main()
{
   // freopen("in.txt","r",stdin);
    point a;
    int t,n;
    int i;
    int p,q,cw,ans;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
    for(i=0;i<t;++i)
    {
        scanf("%d%d%d",&p,&q,&cw);
        a.to=q-1;
        a.w=cw;
        dot[p-1].push_back(a);
    }
    ans=dij(0,n-1);
    printf("%d\n",ans);
    }
    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