航空路线问题[网络流24题之11]
来源:互联网 发布:知画生孩子是第几集 编辑:程序博客网 时间:2024/05/18 03:21
问题描述:
给定一张航空图,图中顶点代表城市,边代表
1 从最西端城市出发,单向从西向东途经若干城市到达最东端城市,然后再单向从东
向西飞回起点(可途经若干城市)。
2 除起点城市外,任何城市只能访问1 次。
编程任务:
对于给定的航空图,试设计一个算法找出一条满足要求的最佳航空旅行路线。
数据输入:
第
再接下来的
结果输出:
输出第
输入文件示例:
8 9
Vancouver
Yellowknife
Edmonton
Calgary
Winnipeg
Toronto
Montreal
Halifax
Vancouver Edmonton
Vancouver Calgary
Calgary Winnipeg
Winnipeg Toronto
Toronto Halifax
Montreal Halifax
Edmonton Montreal
Edmonton Yellowknife
Edmonton Calgary
输出文件示例:
7
Va
ncouver
Edmonton
Montreal
Halifax
To
ronto
Winnipeg
Calgary
Va
ncouver
分析:
题目的输入变态,一堆字符,所以我用了
再说建模,因为除起点外任何城市只能访问
最后求出最小费用最大流即可。
但是,如果图中流量小于
代码:
#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <map>#include <iostream>#include <string>using namespace std;const int inf = 0x3f3f3f3f;map<string,int>turn;queue<int >que;int head[247],nxt[4747],to[4747],wei[4747],cost[4747],cot[4747],tot=1;int dis[247],pre[247],pres[247];bool vis[247];int way[3][247];int n,m;bool flag ;int ans,used;string str[247],s1,s2;void add(int,int,int,int);bool spfa();void doit();void get_way(int,int);int main(){ cin >> n >> m; for(int i=1;i<=n;++i){ cin >> str[i]; turn.insert(pair<string,int>(str[i],i)); if(i!=1 && i!=n) add(i<<1,(i<<1)+1,-1,i); } for(int i=1;i<=m;++i){ cin >> s1 >> s2; if(turn[s1] > turn[s2]) swap(s1,s2); if(turn[s1]==1 && turn[s2]==n) flag = true; add((turn[s1]<<1)+1,turn[s2]<<1,0,-1); } while(ans<2 && spfa()){ doit(); ++ans; } if(flag && !ans) cout << 2 << endl << str[1] << endl << str[n] << endl << str[1]; else if(flag && ans==1){ way[1][++way[1][0]] = 1; get_way(1,3); cout << -used+2 << endl; for(int i=1;i<=way[1][0];++i) cout << str[way[1][i]] << endl; cout << str[n] << endl << str[1]; } else if(ans<2) cout << "No Solution!"; else{ way[1][++way[1][0]] = way[2][++way[2][0]] = 1; get_way(1,3); get_way(2,3); cout << -used+2 << endl; for(int i=1;i<=way[1][0];++i) cout << str[way[1][i]] << endl; cout << str[n] << endl; for(int i=way[2][0];i>=1;--i) cout << str[way[2][i]] << endl; } return 0;}void add(int from,int tp,int spend,int i){ ++tot;nxt[tot]=head[from];head[from]=tot;to[tot]=tp;wei[tot]=1;cost[tot]=spend;cot[tot]=i; ++tot;nxt[tot]=head[tp];head[tp]=tot;to[tot]=from;wei[tot]=0;cost[tot]=-spend;cot[tot]=-1;}bool spfa(){ memset(dis,0x3f,sizeof dis); dis[3] = 0; que.push(3); int now; do{ now = que.front(); vis[now] = false; que.pop(); for(int i=head[now];i;i=nxt[i]) if(dis[to[i]]>dis[now]+cost[i] && wei[i]){ dis[to[i]] = dis[now]+cost[i]; pre[to[i]] = now; pres[to[i]] = i; if(!vis[to[i]]){ vis[to[i]] = true; que.push(to[i]); } } }while(!que.empty()); return dis[n<<1]!=inf;}void doit(){ used += dis[n<<1]; int now = n<<1; while(now != 3){ --wei[pres[now]]; ++wei[pres[now]^1]; now = pre[now]; }}void get_way(int cnt,int place){ if(place == (n<<1)) return ; for(int i=head[place];i;i=nxt[i]) if(!(i&1) && !wei[i] && !vis[to[i]]){ if(cot[i] != -1) way[cnt][++way[cnt][0]] = cot[i]; vis[to[i]] = true; get_way(cnt,to[i]); return ; }}
- 航空路线问题[网络流24题之11]
- 网络流24题之T11 航空路线问题
- 【网络流24题】航空路线问题
- [网络流24题]航空路线问题
- 线性规划与网络流24题 11航空路线问题
- 网络流与线性规划24题11航空路线问题
- [网络流24题 #11]航空路线问题
- 网络流24题11. 航空路线问题
- 「网络流 24 题」航空路线问题
- 【线性规划与网络流24题 11】航空路线
- 线性规划与网络流24——航空路线问题
- 航空路线问题 一般网络流
- 网络流二十四题之十一 —— 航空路线问题(AIRL)
- 【网络流24题】航空路线问题(最大费用流)
- loj6122「网络流 24 题」航空路线问题(最长不相交路径)
- 航空路线问题
- P2770 航空路线问题
- 洛谷 P2770 航空路线问题
- 嵌入式C实战项目开发技巧:如果对一个有规律的数组表进行位移操作
- 浅谈L0,L1,L2范数及其应用
- Android开发中,有哪些让你觉得相见恨晚的方法、类或接口?
- iOS-OC中的lazyLoad(懒加载)
- 基数排序算法(Java语言)
- 航空路线问题[网络流24题之11]
- iOS中UITextField使用详解
- deque容器
- LaTeX数学公式中的空格
- VS2010/MFC 读写excel文件 操作类
- angularjs对json数据的操作--收货地址列表
- 函数返回局部变量的问题
- nsci.txt 是什么?
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场 1001-1006