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

queue + vector 水过。

Posted by dongshimou at 2014-07-21 18:01:55 on Problem 1201
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long
using namespace std;
int n,maxn,minn,m;
struct lx
{
    int v,len;
};
vector<lx>g[51001];

queue<int>q;

void SPFA()
{
    bool vis[51001];
    int dis[51001];
    for(int i=0;i<=maxn;i++)
        vis[i]=0,dis[i]=-INF;
    vis[minn-1]=1,dis[minn-1]=0;
    q.push(minn-1);

    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int j=0;j<g[u].size();j++)
        {
            int v=g[u][j].v;
            int len=g[u][j].len;
//            printf("v=%d,u=%d:%d>%d+%d=\n",v,u,dis[v],dis[u],len);
//                system("pause");
            if(dis[v]<dis[u]+len)
            {
                dis[v]=dis[u]+len;

                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    printf("%d\n",dis[maxn]);
//    for(int i=minn-1;i<=maxn;i++)
//        printf("%d : %d ==\n",i,dis[i]);
}
int main()
{
    while(scanf("%d",&m)!=EOF)
    {
        minn=INF,maxn=0;
        for(int i=0;i<51001;i++)
            g[i].clear();
        int u,v,len;
        lx now;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&len);
            maxn=max(maxn,v);
            minn=min(minn,u);
            now.v=v,now.len=len;
            g[u-1].push_back(now);
        }
        for(int i=minn;i<=maxn;i++)
        {
            now.v=i-1,now.len=-1;
            g[i].push_back(now);
            now.v=i,now.len=0;
            g[i-1].push_back(now);
        }
//        for(int i=minn-1;i<=maxn;i++)
//        {
//            printf("%d-> ",i);
//            for(int j=0;j<g[i].size();j++)
//                printf("%d:%d  ",g[i][j].v,g[i][j].len);
//            printf("\n");
//        }
        while(!q.empty())q.pop();
        SPFA();
    }
}

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