题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的,

问你从s到t最短时间是多少。

析:使用dijkstra算法,从每个结点出发,求最短路,并维护时间的最小值,这个可以用优先队列,然后考虑能不能通过这条路,如果t<a,可以在输入时处理。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 300 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int to, a, b, t;
};
vector<Node> G[maxn];
int d[maxn]; int dijkstra(int s, int ttt){
priority_queue<P, vector<P>, greater<P> > pq;
pq.push(P(0, s));
fill(d, d+n+1, INF);
d[s] = 0; while(!pq.empty()){
P p = pq.top(); pq.pop();
if(p.second == ttt) return p.first;
int v = p.second;
if(d[v] < p.first) continue;
for(int i = 0; i < G[v].size(); ++i){
Node &u = G[v][i];
int t = p.first % (u.a+u.b);
if(t + u.t <= u.a && d[u.to] > d[v] + u.t){
d[u.to] = d[v] + u.t;
pq.push(P(d[u.to], u.to));
}
else if(t + u.t > u.a){
int tt = u.a + u.b - t + u.t;
if(d[u.to] > d[v] + tt){
d[u.to] = d[v] + tt;
pq.push(P(d[u.to], u.to));
}
}
}
}
return 0;
} int main(){
int kase = 0;
int s, t;
while(scanf("%d %d %d %d", &n, &m, &s, &t) == 4){
int u, v, a, b, tt;
for(int i = 1; i <= n; ++i) G[i].clear();
while(m--){
scanf("%d %d %d %d %d", &u, &v, &a, &b, &tt);
if(tt > a) continue;
G[u].push_back((Node){v, a, b, tt});
}
printf("Case %d: %d\n", ++kase, dijkstra(s, t));
}
return 0;
}

最新文章

  1. LingQ 的Distinct使用方法
  2. androidstudio 配置git和github
  3. Java虚拟机8:虚拟机性能监控与故障处理工具
  4. iOS异步下载下载进度条显示
  5. ios swfit 由继承UIButton了解类的构造方法
  6. wireshark抓包直观图解 TCP三次握手/四次挥手详解
  7. wpf图片切换,幻灯效果
  8. 集合框架工具类--Collections排序
  9. hadoop多机安装HA+YARN
  10. guestmount
  11. RUP(Rational Unified Process)笔记整理
  12. HDU 2802 F(N)(简单题,找循环解)
  13. 使用关系型数据库作为Redis落地的思路
  14. 调用Class.forName()要抛出异常
  15. Java的IO系统
  16. 问题:AJAX的send参数里,空格以及它后面的数据在传递时消失(已解决)
  17. (转载)dotnet core 中文乱码 codepages
  18. Springmvc的原理和业务处理
  19. linus jsch文件下载
  20. 用java数组模拟购物商城功能与实现

热门文章

  1. 矩形嵌套-记忆化搜索(dp动态规划)
  2. 关于arr.map()问题
  3. CASIO fx-991es Plus科学计算器使用技巧
  4. poj3034--Whac-a-Mole(dp)
  5. 从士兵到程序员再到SOHO程序员 (二)
  6. python的id()函数的一个小方面(转载)
  7. 2015最新iherb海淘攻略-图文入门教程-6月免邮
  8. WPF popup控件的使用
  9. EasyDarwin手机直播是如何实现的快速显示视频的方法
  10. BZOJ2759: 一个动态树好题