标签:style blog http color io os ar for sp
最大流 。以航班为节点进行最大流。 容量限制进行拆点。 如果时间地点满足可以建一条边。 具体看代码。变量名被修改过了。一开始的变量名可能比较容易看懂
但CE了。可能与库里的变量重复了。
AC代码
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} #define MAXN 15050 const int INF = 0x3f3f3f3f ; struct node { int u,v,next; int cap,flow; }edge[1000005]; int cnt,N,M; int head[MAXN]; int ans; string stpos,edpos,sti,eti; map<string,int>place; struct flight { int s,t; int statime,ndtime; int num; }src[MAXN]; int cas,lasttime,a[MAXN]; queue<int>q; void addedge(int u, int v, int cap) { edge[cnt].u = u; edge[cnt].v = v; edge[cnt].cap = cap; edge[cnt].flow = 0; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].cap = 0; edge[cnt].flow = 0; edge[cnt].next = head[v]; head[v] = cnt++; } void read() { place.clear(); cas = 1; cin >> stpos >> edpos; place[stpos] = cas++; place[edpos] = cas++; cin >> eti; lasttime = ((eti[0] - ‘0‘) * 10 + eti[1] - ‘0‘) * 60 + (eti[2] - ‘0‘) * 10 + eti[3] - ‘0‘; cin >> M; for (int i = 1; i <= M; i++) { cin >> stpos >> edpos; if (!place[stpos]) place[stpos] = cas++; if (!place[edpos]) place[edpos] = cas++; cin >> src[i].num; cin >> sti >> eti; src[i].statime = ((sti[0] - ‘0‘) * 10 + sti[1] - ‘0‘) * 60 + (sti[2] - ‘0‘) * 10 + sti[3] - ‘0‘; src[i].ndtime = ((eti[0] - ‘0‘) * 10 + eti[1] - ‘0‘) * 60 + (eti[2] -‘0‘) * 10 + eti[3] - ‘0‘; src[i].s = place[stpos]; src[i].t = place[edpos]; } } void build() { memset(head,-1,sizeof(head)); cnt = 0; for (int i = 1; i <= M; i++) { if (src[i].s == 1) addedge(0,i,INF); if (src[i].t == 2 && src[i].ndtime <= lasttime) addedge(i + M,M * 2 + 1,INF); addedge(i, i + M, src[i].num); for (int j = 1; j <= M; j++) { if (i == j)continue; if (src[i].t == src[j].s && src[i].ndtime + 30 <= src[j].statime) addedge(i + M, j,INF); } } } int Edmonds_karp(int source,int target) { while (!q.empty()) q.pop(); int p[MAXN]; int F = 0; while (true) { memset(p,-1,sizeof(p)); q.push(source); memset(a,0,sizeof(a)); a[source] = INF; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (!a[v] && edge[i].cap > edge[i].flow) { a[v] = min(a[u],edge[i].cap - edge[i].flow); p[v] = i; q.push(v); } } } if (a[target] == 0) break; for (int i = p[target]; i != -1; i = p[edge[i].u]) { edge[i].flow += a[target]; edge[i ^ 1].flow -= a[target]; } F += a[target]; } return F; } int main() { //freopen("sample.txt","r",stdin); while (scanf("%d",&N) != EOF) { read(); build(); printf("%d\n",Edmonds_karp(0, 2 * M + 1)); } return 0; }
CE代码也放在这里容易看懂一些
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} #define MAXN 15050 const int INF = 0x3f3f3f3f ; struct node { int u,v,next; int cap,flow; }edge[1000005]; int cnt,N,M; int head[MAXN]; int ans; string stpos,edpos,stime,etime; map<string,int>place; struct flight { int s,t; int sttime,edtime; int num; }src[MAXN]; int cas,lasttime,a[MAXN]; queue<int>q; void addedge(int u, int v, int cap) { edge[cnt].u = u; edge[cnt].v = v; edge[cnt].cap = cap; edge[cnt].flow = 0; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].cap = 0; edge[cnt].flow = 0; edge[cnt].next = head[v]; head[v] = cnt++; } void read() { place.clear(); cas = 1; cin >> stpos >> edpos; place[stpos] = cas++; place[edpos] = cas++; cin >> etime; lasttime = ((etime[0] - ‘0‘) * 10 + etime[1] - ‘0‘) * 60 + (etime[2] - ‘0‘) * 10 + etime[3] - ‘0‘; cin >> M; for (int i = 1; i <= M; i++) { cin >> stpos >> edpos; if (!place[stpos]) place[stpos] = cas++; if (!place[edpos]) place[edpos] = cas++; cin >> src[i].num; cin >> stime >> etime; src[i].sttime = ((stime[0] - ‘0‘) * 10 + stime[1] - ‘0‘) * 60 + (stime[2] - ‘0‘) * 10 + stime[3] - ‘0‘; src[i].edtime = ((etime[0] - ‘0‘) * 10 + etime[1] - ‘0‘) * 60 + (etime[2] - ‘0‘) * 10 + etime[3] - ‘0‘; src[i].s = place[stpos]; src[i].t = place[edpos]; } } void build() { memset(head,-1,sizeof(head)); cnt = 0; for (int i = 1; i <= M; i++) { if (src[i].s == 1) addedge(0,i,INF); if (src[i].t == 2 && src[i].edtime <= lasttime) addedge(i + M,M * 2 + 1,INF); addedge(i, i + M, src[i].num); for (int j = 1; j <= M; j++) { if (i == j)continue; if (src[i].t == src[j].s && src[i].edtime + 30 <= src[j].edtime) addedge(i + M, j,INF); } } } int Edmonds_karp(int source,int target) { while (!q.empty()) q.pop(); int p[MAXN]; int F = 0; while (true) { memset(p,-1,sizeof(p)); q.push(source); memset(a,0,sizeof(a)); a[source] = INF; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (!a[v] && edge[i].cap > edge[i].flow) { a[v] = min(a[u],edge[i].cap - edge[i].flow); p[v] = i; q.push(v); } } } if (a[target] == 0) break; for (int i = p[target]; i != -1; i = p[edge[i].u]) { edge[i].flow += a[target]; edge[i ^ 1].flow -= a[target]; } F += a[target]; } return F; } int main() { //freopen("sample.txt","r",stdin); while (scanf("%d",&N) != EOF) { read(); build(); printf("%d\n",Edmonds_karp(0, 2 * M + 1)); } return 0; }
UVALIVE 3645 Objective: Berlin
标签:style blog http color io os ar for sp
原文地址:http://www.cnblogs.com/Commence/p/4028628.html