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 jwzxgo at 2012-03-15 21:01:44 on Problem 1860
如果把手上的货币种类不同看成不同的状态(可以证明最优解存在时一定是每次都把本钱全部投入的),不同的交易方式看成状态迁移的路径,那么这道题等价于在directed multigraph中的回路检验。
算法:Bellman-ford
着色版代码:http://ideone.com/W9kMG
//POJ 1860
#include <stdio.h>
#include <assert.h>
 
struct Edge{
        int src, dest; 
        double r, c;
} edges[200];           // Direct Multigraph, each input line = 2 edges
double vertices[100];   // amount of currency, information of parent is not important
int N, B, S; 
double V;
 
double max( double a, double b ){ return (a>b)? a:b; }
 
int hasCircle(){ // Bellman Ford
        int i, j;
        for( i=0; i<N-1; i++ ){
                for( j=0; j<2*B; j++ ){         // 2*B!
                        vertices[edges[j].dest] = max( vertices[edges[j].dest], (vertices[edges[j].src] - edges[j].c) * edges[j].r );
                }
        }
        for( j=0; j<2*B; j++ ){
                if( vertices[edges[j].dest] < (vertices[edges[j].src] - edges[j].c) * edges[j].r ){     return 1;       }
        }
        return 0;
}
void init(){
        int i;
        for( i=0; i<B; i++ ){ vertices[i]=0; }
        vertices[S-1] = V;
}
void read(){
        scanf("%d%d%d%lf", &N, &B, &S, &V);
        int i;
        for( i=0; i<B; i++ ){
                int a, b; 
                double r1, c1, r2, c2;
                scanf("%d%d%lf%lf%lf%lf", &a, &b, &r1, &c1, &r2, &c2);
                edges[2*i].src=a-1; edges[2*i].dest=b-1; edges[2*i].r=r1; edges[2*i].c=c1; 
                edges[2*i+1].src=b-1; edges[2*i+1].dest=a-1; edges[2*i+1].r=r2; edges[2*i+1].c=c2; 
        }
}
int main(){
 
        read(); 
        init(); 
        if( hasCircle() == 1 ){ 
                printf("YES\n"); 
        } else { 
                printf("NO\n"); 
        }
        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