题目:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333
题意:
分析:spfa
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long LL;
int N,M,X,Y;
const int maxn = 306;
const LL INF = 2e16;
LL cost[maxn];
bool visit[maxn];
queue <int > q;
struct node
{
int pos,a,b,t;
int next;
}List[100006];
int Table[maxn],cnt;
void Init()
{
cnt=0;
memset(Table,-1,sizeof(Table));
memset(visit,0,sizeof(visit));
fill(cost,cost+maxn,INF);
}
void add(int u,int v,int a,int b,int t)
{
List[cnt].pos=v;
List[cnt].a=a;
List[cnt].b=b;
List[cnt].t=t;
List[cnt].next=Table[u];
Table[u]=cnt++;
}
inline void slack(int u,int v,int w) //松弛 u-->v
{
if(cost[v]>cost[u]+w)
{
cost[v]=cost[u]+w;
if(!visit[v])
{
visit[v]=true;
q.push(v);
}
}
}
inline LL calcost(int now,int a,int b,int t)
{
now=now%(a+b);
if(now+t<=a)
return t;
return a+b-now+t;
}
void spfa()
{
q.push(X);
cost[X]=0;
visit[X]=true;
int p,temp;
while(!q.empty())
{
temp=q.front();
q.pop();
visit[temp]=false;
for(p=Table[temp];~p;p=List[p].next)
slack(temp,List[p].pos,calcost(cost[temp],List[p].a,List[p].b,List[p].t));
}
}
int main()
{
int u,v,a,b,t,ncase=1;
while(scanf("%d%d%d%d",&N,&M,&X,&Y)!=EOF)
{
Init();
while(M--)
{
scanf("%d%d%d%d%d",&u,&v,&a,&b,&t);
if(t>a)
continue ;
add(u,v,a,b,t);
}
spfa();
printf("Case %d: %lld\n",ncase++,cost[Y]);
}
return 0;
}