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

晕死,无解时输出了INF~~还好然后110ms A掉了,spfa做的~

Posted by Moon_1st at 2011-02-01 16:02:42 on Problem 1158
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

const int INF = 1000000000;
const int N = 301;

typedef vector<int> VCT;
vector <VCT> g(N);

int n, m, s, t, dist[N], r[N], tb[N], tp[N], map[N][N];
char c[N];
bool visit[N];

int Min(int x, int y)
{
    return x < y ? x : y;
}

char Getcolor(int x, int dis, int &rt)
{
    int rest;
    if(dis < r[x])
    {
        rt = r[x] - dis;
        return c[x];
    }
    else if(dis == r[x])
         {
             if(c[x] == 'B')
             {
                 rt = tp[x];
                 return 'P';
             }
             else
             {
                 rt = tb[x];
                 return 'B';
             }
         }
    rest = (dis-r[x]) % (tb[x]+tp[x]);
    if(c[x] == 'B')
    {
        if(rest > tp[x])
        {
            rt = tb[x]+tp[x]-rest;
            return 'B';
        }
        else
        {
            rt = tp[x] - rest;
            return 'P';
        }
    }
    else
    {
        if(rest > tb[x])
        {
            rt = tb[x]+tp[x]-rest;
            return 'P';
        }
        else
        {
            rt = tb[x] - rest;
            return 'B';
        }
    }
}

int Same(int u, int v, int dis)
{
    char cu, cv;
    int tu, tv;
    cu = Getcolor(u, dis, tu);
    cv = Getcolor(v, dis, tv);
    if(cu == cv)  return 0;
    else
    {
        if(tu != tv)  return Min(tu, tv);
        else
        {
            if(cu == 'B')
            {
                if(tp[u] != tb[v])  return tu+Min(tp[u], tb[v]);
                else if(tb[u] != tp[v])  return tu+tp[u]+Min(tb[u], tp[v]);
                else  return -1;
            }
            else
            {
                if(tb[u] != tp[v])  return tu+Min(tb[u], tp[v]);
                else  if(tp[u] != tb[v])  return tu+tb[u]+Min(tp[u], tb[v]);
                else  return -1;
            }
        }
    }
}

void spfa()
{
    int i, u, v, len, tmp;
    queue<int> q;
    while(!q.empty())  q.pop();
    q.push(s);
    memset(visit, false, sizeof(visit));
    for(i = 1; i <= n; i++)  dist[i] = INF;
    dist[s] = 0;
    while(!q.empty())
    {
        u = q.front();
        visit[u] = false;
        q.pop();
        len = g[u].size();
        for(i = 0; i < len; i++)
        {
            v = g[u][i];
            tmp = Same(u, v, dist[u]);
            if(tmp!=-1 && dist[v]>dist[u]+map[u][v]+tmp)
            {
                dist[v] = dist[u] + map[u][v]+tmp;
                if(!visit[v])
                {
                    visit[v] = true;
                    q.push(v);
                }
            }
        }
    }
    if(dist[t] != INF)  printf("%d\n", dist[t]);
    else  printf("0\n");
}


int main()
{
    int i, x, y, z;
    scanf("%d%d%d%d", &s, &t, &n, &m);
    for(i = 1; i <= n; i++)  g[i].clear();
    for(i = 1; i <= n; i++)
        scanf(" %c%d%d%d", &c[i], &r[i], &tb[i], &tp[i]);
    for(i = 0; i < m; i++)
    {
        scanf("%d%d%d", &x, &y, &z);
        g[x].push_back(y);
        g[y].push_back(x);
        map[x][y] = map[y][x] = z;
    }
    spfa();
    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