您的位置:首页 > 移动开发 > Objective-C

UVA 1161&& UVALive 3645 Objective: Berlin (最大流 -- 时序模型)

2016-10-29 10:59 519 查看
大体题意:

给你一些航班,你要从s 城市到t 城市,中间可以倒航班,倒航班的时间不得小于30分钟!  告诉你最晚的到达时间,求最大客流量?

思路:

求最大客流量很明显是求最大流!

然后 加了时间 在赛场上就蒙蔽了!

其实是时序模型!

我们把航班看成一个点,然后拆成i和i + m,这样就考虑了时间的因素! 表示i 的时间更早一些,i+m 的时间更晚一些!只能由i到i+m  ,容量为飞机上座位数,如果这个航线连接着起点,那么就让i 连接超级源点,容量为inf,如果这个航线连接着 终点,就让i + m 连接者超级终点 ,容量为inf 那么  超级原点和超级终点的最大流就是答案!

因为有反向弧的存在,注意数组大小要合理!

详细见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <queue>
using namespace std;
const int maxn = 10000 + 7;
const int inf = 0x3f3f3f3f;
int cnt;
map<string,int> mp;
int ID(string s){
if (!mp.count(s)) return mp[s] = ++cnt;
return mp[s];
}

struct Edge{
int from, to, cap, flow;
};

struct Dinic{
int n, m, s, t;
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];

void init(int nn){
edges.clear();
for (int i = 0; i < nn; ++i)G[i].clear();
// memset(vis,0,sizeof vis);
// memset(d,0,sizeof d);
// memset(cur,0,sizeof cur);
}

void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}

bool BFS(){
memset(vis,0,sizeof vis);
queue<int>Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for (int i = 0; i < G[x].size(); ++i){
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}

int DFS(int x,int a){
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); ++i){
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to,min(a,e.cap-e.flow))) > 0){
e.flow += f;
edges[G[x][i]^1 ].flow -= f;
flow += f;
a -= f;
if (!a)break;

}

}
return flow;
}

int Maxflow(int s,int t){
this->s = s; this->t = t;
int flow = 0;
while(BFS()){
memset(cur,0,sizeof cur);
flow += DFS(s,inf);
}
return flow;
}

}dic;
char name1[10],name2[10], t1[10];
int rev(char *s){
return 60*((s[0]-48) * 10 + s[1] - 48) + 10*(s[2]-48)+s[3]-48;
}
struct Node{
int u,v,t,t1,t2;

}p[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
cnt = 0;
mp.clear();
scanf("%s%s",name1,name2);
ID(name1); ID(name2);
scanf("%s",t1);
int Time = rev(t1);
int m;
scanf("%d",&m);
char Start[10],End[10];
for (int i = 1; i <= m; ++i){
scanf("%s%s",Start,End);
p[i].u = ID(Start);
p[i].v = ID(End);
scanf("%d",&p[i].t);
scanf("%s%s",Start,End);
p[i].t1 = rev(Start);
p[i].t2 = rev(End);
}
dic.init(2*m+10);
for (int i = 1; i <= m; ++i){
dic.AddEdge(i,i+m,p[i].t);
if (p[i].u == 1){
dic.AddEdge(0,i,inf);
}
if (p[i].v == 2 && p[i].t2 <= Time) dic.AddEdge(i+m,2*m+1,inf);
for (int j = 1; j <= m; ++j){
if (p[i].v == p[j].u && p[i].t2 + 30 <= p[j].t1 && i != j){
dic.AddEdge(i+m,j,inf);
}
}
}
printf("%d\n",dic.Maxflow(0,2*m+1));
}
return 0;
}
/**
4
lisbon
berlin
1500
9
lisbon london 6 1000 1100
london lisbon 6 1130 1230
lisbon paris 5 1000 1100
paris lisbon 4 1130 1230
london paris 1 1130 1300
london berlin 2 1340 1510
berlin london 2 1300 1430
paris berlin 10 1330 1500
berlin paris 9 1300 1430

**/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: