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

一次A过,留下代码做纪念~(bellman-ford)

Posted by sunyi740 at 2016-03-24 22:17:29 on Problem 3259
#include<iostream>
#include<cstring>
using namespace std;
int dis[6000];
int mix=10005;
int n,m,w;
struct edge
{
    int u;
    int v;
    int t;
};
edge a[6000];

void bellman()
{
    memset(dis,mix,sizeof(dis));
    dis[1]=0;
    for(int i=1;i<n-1;i++)
        for(int j=1;j<=2*m+w;j++)
        {
            if(dis[a[j].u]+a[j].t<dis[a[j].v])
                dis[a[j].v]=dis[a[j].u]+a[j].t;
        }
    int flag=0;
    for(int j=1;j<=2*m+w;j++)
    {
        if(dis[a[j].u]+a[j].t<dis[a[j].v])
        {
            flag=1;
            break;
        }
    }
    if(flag==1) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return;
}

int main()
{
    int f;
    cin>>f;
    while(f--)
    {
        cin>>n>>m>>w;
        int i;
        for(i=1;i<=2*m-1;i=i+2)
        {
            cin>>a[i].u>>a[i].v>>a[i].t;
            a[i+1].u=a[i].v;
            a[i+1].v=a[i].u;
            a[i+1].t=a[i].t;
        }
        for(i=2*m+1;i<=2*m+w;i++)
        {
            cin>>a[i].u>>a[i].v>>a[i].t;
            a[i].t=-a[i].t;
        }
        bellman();
    }
    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