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

路过的好心人,帮忙看看哪里错了

Posted by lizeliang at 2009-08-14 11:26:27 on Problem 1135 and last updated at 2009-08-14 11:27:52
//写的比较麻烦,是想用用刚学的priority_queue和数组存边

#include<iostream>
#include<mem.h>
#include<queue>
#include<vector>
#include<algorithm>
#include<iomanip>
#define big 999999999.9
using namespace std;
struct node        //边性质
{
    int from,to;
    double w;
};
node edg[250005];       //记录边
double in[130005];       //记录边内点的推倒时间
bool operator < (node a,node b)
{
    if(a.from==b.from)
        return a.to<b.to;
    return a.from<b.from;
}
struct cmp
{
    bool operator () (node a,node b)
    {
        return a.w>b.w;
    }  
};
int index[505];   //第i个点推导后,会直接推倒edg[j].to(j:index[i]-index[i+1])
double key[505];             //点被推倒的时间
bool flag[505];
int main()
{
    int n,edge,test=0;
    priority_queue <node,vector<node>,cmp> q;
    while(cin>>n>>edge,n)
    {
        test++;
        cout<<"System #"<<test<<endl;
        if(n==1 && edge==0)
        {
            cout<<"The last domino falls after "
                <<setiosflags(ios::fixed)<<setprecision(1)
                <<0.00<<" seconds, at key domino 1."<<endl;
            continue;
        }
        memset(flag,0,sizeof(flag));
        for(int i=0;i<=n;i++)
            key[i]=big;
        for(int i=0;i<edge;i++)
        {
            cin>>edg[i].from>>edg[i].to>>edg[i].w;
            edg[i+edge].from=edg[i].to;
            edg[i+edge].to=edg[i].from;
            edg[i+edge].w=edg[i].w;
        } //将无向图变成有向图
        sort(edg,edg+2*edge);
        index[edg[2*edge-1].from+1]=2*edge;
        for(int i=2*edge-1;i>=0;i--)
            index[edg[i].from]=i;
         //建立索引 
         //dijkstra:
        int now=1;
        key[now]=0;
        while(1)
        {
            flag[now]=true;
            for(int i=index[now];i<index[now+1];i++)
            {
                if(key[edg[i].to]>key[now]+edg[i].w)
                {
                    key[edg[i].to]=key[now]+edg[i].w;
                    q.push( (node){ edg[i].from , edg[i].to , edg[i].w} );
                }
            }
            node temp;
            while(!q.empty())
            {
                temp=q.top();
                q.pop();
                if(flag[temp.to]!=true && flag[temp.from]!=false)
                    break;
            }
            if(flag[temp.to]!=true)
                now=temp.to;
            else
                break;
        } //求出点的推倒时间
         //求边内点的推到时间 
        for(int i=0;i<2*edge;i++)
        {
            double la,sm;
            if(key[edg[i].from]>key[edg[i].to])
            {
                la=key[edg[i].from];
                sm=key[edg[i].to];
            }
            else
            {
                sm=key[edg[i].from];
                la=key[edg[i].to];
            }
            if(sm+edg[i].w<=la)
                in[i]=sm;
            else
                in[i]=la+(edg[i].w+sm-la)/2;
        }
         //找到最后倒的点
        int posv,posi;
        double maxv=-big,maxi=-big;
        for(int i=1;i<=n;i++)
            if(key[i]>maxv)
            {
                maxv=key[i];
                posv=i;
            }
        for(int i=0;i<2*edge;i++)
            if(in[i]>maxi)
            {
                maxi=in[i];
                posi=i;
            }
        if(key[posv]>=in[posi])
            cout<<"The last domino falls after "
                <<setiosflags(ios::fixed)<<setprecision(1)
                <<key[posv]<<" seconds, at key domino "<<posv<<"."<<endl;
        else
        {
            int a=edg[posi].from,b=edg[posi].to;
            if(a<b)
            {
                int temp=a;
                a=b;
                b=temp;
            }
            cout<<"The last domino falls after "
                <<setiosflags(ios::fixed)<<setprecision(1)<<in[posi]
                <<" seconds, between key dominoes "<<b
                <<" and "<<a<<"."<<endl;
        }
        cout<<endl;
    }
    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