CSU - 1333 1333: Funny Car Racing(spfa)

时间:2023-03-09 16:01:40
CSU - 1333 1333: Funny Car Racing(spfa)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333

这题多了一个限制条件是每一条路都会规律的开放a时间关闭b时间,车子必须在开放的时候进入,在关闭之前出来,那么在加边的时候只要权值>开放时间的就不用加进去了.

还有一个问题是重边,那么用邻接表存储就好,并且这题不用spfa就超时,至于判断到达某一个点的时间,只要比较dist[x]%(temp.a+temp.b)的数即可.

算出来之后a需要判断是否还能够在剩余时间通过这个路口,不然就需要等待。还有算出需要等待的时间后必须判断是不是能更新目标点,否则会wa.

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include <deque>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define INF 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 310
#define maxv 50010
#define mod 1000000000
using namespace std; struct edge
{
int to,weight,a,b;
};
vector<edge>adjmap[maxv]; //动态邻接表
bool in_queue[maxn]; //顶点是否在队列中
int dist[maxn];//源点到各点的最短路径
int nodesum; //顶点数
int edgesum; //边数
int S,T;//起点和终点 void SPFA(int source)
{
deque<int>dq;
int i,j,x,to;
for(int i=;i<=nodesum;i++)
{
in_queue[i]=false;
dist[i]=INF;
}
dq.push_back(source);
dist[source]=;
in_queue[source]=true;
//初始化 完成
while(!dq.empty())
{
x=dq.front(); //取出队首元素
dq.pop_front();
in_queue[x]=false; //访问标记置0 ,同一个顶点可以访问多次,只要dist值变小
for(i=;i<adjmap[x].size();i++)
{
to=adjmap[x][i].to;
edge temp=adjmap[x][i];
// cout<<temp.a<<" "<<temp.b<<endl;
if((dist[x]<INF)&&(dist[to]>dist[x]+temp.weight))
{
int t=dist[x]%(temp.a+temp.b),t1;
//cout<<dist[x]<<" "<<t<<endl;
if(t<=temp.a)
{
if(temp.a-t-temp.weight>=) t1=temp.weight;
else t1=temp.a-t+temp.b+temp.weight;
}
else t1=temp.a+temp.b-t+temp.weight;
// cout<<t1<<endl;
if(dist[to]>dist[x]+t1) dist[to]=dist[x]+t1;
else continue; //注意这里
if(!in_queue[to])
{
in_queue[to]=true;
if(!dq.empty())
{
if(dist[to]>dist[dq.front()]) dq.push_back(to); //大的加入 队尾
else dq.push_front(to); //小的加入队首
}
else dq.push_back(to); //队列为空加入队尾
}
}
}
}
} int main()
{
//Read;
int i,s,e,w,x,y,j=;
edge temp;
while(cin>>nodesum>>edgesum>>S>>T)
{
for(int i=;i<=nodesum;i++) adjmap[i].clear();
for(int i=;i<=edgesum;i++)
{
cin>>s>>e>>x>>y>>w;
if(w<=x)
{
temp.to=e;
temp.weight=w;
temp.a=x,temp.b=y;
adjmap[s].push_back(temp);
}
}
SPFA(S);
cout<<"Case "<<j++<<":"<<" "<<dist[T]<<endl;
}
return ;
}