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

Re:FOXLIU的好方法,让我过了

Posted by hjp_test at 2011-07-04 09:02:17 on Problem 1860
In Reply To:请问下有没有哪位朋友用spfa过的?代码借看下。 Posted by:sword_snow at 2010-07-02 15:08:37
> 我的总是WA
> #include<iostream>
> using namespace std;
> #include<cmath>
> #include<algorithm>
> #include<cstring>
> #include<stdio.h>
> #include<string>
> #include<map>
> #include<set>
> #include<queue>
> double rate[101][101],c[101][101],r1,r2,c1,c2;
> double d[101];
> double vs;
> #define eps 1e-8
> bool relax(int u,int v)
> {
> 	if (d[v]+eps<(d[u]-c[u][v])*rate[u][v]){ 
> 		d[v]=(d[u]-c[u][v])*rate[u][v];
> 		return true;
> 	}
> 	else return false;
> }
> bool spfa(int s,int n)
> {
> 	int f=0,r=1,p=1,i,j;
> 	int q[101],t[101];
> 	bool in[101];
> 	memset(in,false,sizeof(in));
> 	memset(t,0,sizeof(t));
> 	q[1]=s;
> 	in[s]=true;
> 	t[s]++;
> 	while (f!=r)
> 	{
> 		f=f%n+1;
> 		int front=q[f];
> 		in[front]=false;
> 		for (i=1;i<=n;i++)
> 			if ((!in[i])&&rate[front][i]>eps)
> 			{
> 				if (relax(front,i)) {
> 					in[i]=true,r=r%n+1,q[r]=i,t[i]++;
> 					if (i==s) return false;
> 				}
> 				if (t[i]>n) return false;
> 			}
> 	}
> 	 if (d[s]>vs+eps) return false;
> 	 else return true;
> }
> int main(){
> 	int i,j,n,m,s,a,b;
> 	memset(d,0,sizeof(d));
> 	memset(rate,0,sizeof(rate));
> 	memset(c,0,sizeof(c));
> 	scanf("%d",&n);
> 	scanf("%d",&m);
> 	scanf("%d",&s);
> 	scanf("%lf",&vs);
> 	//cout<<v;
> 	for (i=0;i<m;i++)
> 	{
> 		scanf("%d %d",&a,&b);
> 		scanf("%lf %lf %lf %lf",&r1,&c1,&r2,&c2);
> 		rate[a][b]=r1;rate[b][a]=r2;c[a][b]=c1;c[b][a]=c2;
> 	}
> 	d[s]=vs;
> 	if (spfa(s,n)) cout<<"NO\n";
> 	else cout<<"YES\n";
> 	return 0;
> }


#include <cstdio>
#include <queue>
using namespace std;

#define EXPNTNUM (100 + 10)
#define CURNUM (100 + 10)

struct Node {
    int to;
    Node* next;
    double commission, ratio;
};

Node nodeHead[CURNUM + 1];
Node edge[EXPNTNUM * 2 + 2];
int pos = 0;
Node* getEdge() {
    return edge + pos++;
}

double maxWeight[CURNUM + 1];

void addEdge(int from, int to, double ratio, double commission) {
    Node* e = getEdge();
    Node* n = nodeHead + from;
    e->to = to;
    e->ratio = ratio;
    e->commission = commission;
    e->next = n->next;
    n->next = e;
}

int main() {
    int currencyNum, pointNum, currencyNumHas;
    int i, j, a, b;
    double ratioab, ratioba, comab, comba;
    double currencisHas;

    scanf("%d%d%d%lf", &currencyNum, &pointNum, &currencyNumHas, &currencisHas);
    for (i = 1; i <= currencyNum; ++i) {
        maxWeight[i] = 0;
        nodeHead[i].next = NULL;
    }
    for (i = j = 0; i < pointNum; ++i) {
        scanf("%d%d", &a, &b);
        scanf("%lf%lf%lf%lf", &ratioab, &comab, &ratioba, &comba);
        addEdge(a, b, ratioab, comab);
        addEdge(b, a, ratioba, comba);
    }

    maxWeight[currencyNumHas] = currencisHas;

    queue<int> q;
    q.push(currencyNumHas);
    int maxEdgeCount = 0;
    while (true) {
        int u = q.front();
        q.pop();
        for (Node* tra = nodeHead[u].next; tra != NULL; tra = tra->next) {
            double temp = (maxWeight[u] - tra->commission) * 
                tra->ratio;
            if (maxWeight[tra->to] < temp) {
                maxWeight[tra->to] = temp;
                q.push(tra->to);
            }
        }
        maxEdgeCount++;
        if (maxWeight[currencyNumHas] > currencisHas) {
            printf("YES\n");
            break;
        }
        if (q.empty()) {
            printf("NO\n");
            break;
        }
    }

    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