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 resty at 2008-05-28 11:45:32 on Problem 2240
晕死了,一直超时。难道是不能用cin或是string?
#include <iostream>
#include <string>
using namespace std;
int const maxv = 32;
struct enode{
       int a, b;
       double c;
};

enode e[10000];

string c[maxv];
int a[maxv][maxv];
int tmp[maxv];
double dist[maxv];

bool bellman_ford(int n, int m){   
     for (int i = 0; i < n; i++) dist[i] = 1;
     for (int j = 0; j <= n; j ++ ){
         int p = 0;
         for (int i = 0; i < m; i++) {
             double temp = dist[e[i].a] * e[i].c;
             if (dist[e[i].a] * e[i].c > dist[e[i].b])
                dist[e[i].b] = dist[e[i].a] * e[i].c , p = 1;
             }
         if (p == 0) return false;
     }
     return true;
}

int main(){
    int n, tt = 0;
    while (cin >> n, n!=0){
          tt++;
          for (int i = 0; i < n; i++) cin >> c[i];
          int m;
          cin >> m; int l = 0;
          for (int i = 0; i < m; i++) {
              memset(a, 0, sizeof(a));
              string s1, s2;
              int x, y; double p;
              cin >> s1 >> p >> s2;
              for (int j = 0; j < n; j++) if (s1 == c[j]) {x = j; break;}
              for (int j = 0; j < n; j++) if (s2 == c[j]) {y = j; break;}
              if (a[x][y] > 0){   // Keep the max cost
                 int k = a[x][y];
                 if (e[k].c  < p) e[k].c = p;
              } else {
                 e[l].a = x; e[l].b =y; e[l].c = p;
                 a[x][y] = l;
                 l++;
              }
          }
          cout << "Case " <<tt << ": " <<( bellman_ford(n, l)? "Yes" : "No") << 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