UVA 12661(p375)----Funny Car Racing

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=500;
struct Edge
{
    int v,open,close,need;
    Edge(int a,int b,int c,int d):v(a),open(b),close(c),need(d){}
};
int cas=0,n,m,s,t;
int dist[maxn],inq[maxn];
vector<Edge>g[maxn];
queue<int> q;
void solve(int s)
{
    memset(dist,63,sizeof(dist));
    memset(inq,0,sizeof(inq));
    while(!q.empty()) q.pop();
    dist[s]=0;
    q.push(s);
    inq[s]=true;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0; i<g[u].size(); i++)
        {
            int sum=g[u][i].open+g[u][i].close;
            int temp=dist[u]/sum;int wait;
            int time=dist[u]-temp*sum;
            if(time>=0&&time<=g[u][i].open)
            {
                if(time+g[u][i].need<=g[u][i].open) wait=0;
                else wait=sum-time;
            }
            else wait=sum-time;
            if(dist[u]+g[u][i].need+wait<dist[g[u][i].v])
            {
                dist[g[u][i].v]=dist[u]+g[u][i].need+wait;
                if(!inq[g[u][i].v])
                {
                    inq[g[u][i].v]=true;
                    q.push(g[u][i].v);
                }
            }
        }
        inq[u]=false;
    }
    printf("Case %d: %d\n",++cas,dist[t]);
}
int main()
{
    while(scanf("%d%d%d%d",&n,&m,&s,&t)==4)
    {
        for(int i=1;i<=n;i++) g[i].clear();
        for(int i=0; i<m; i++)
        {
            int u,v,a,b,c;
            scanf("%d%d%d%d%d",&u,&v,&a,&b,&c);
            if(a>=c) g[u].push_back(Edge(v,a,b,c));
        }
        solve(s);
    }
        return 0;
    }

你可能感兴趣的:(UVA 12661(p375)----Funny Car Racing)