FZU 1852 Impossible Mission II floyd+TSP
2016-07-27 15:05
344 查看
Problem 1852 Impossible Mission II
Accept: 76 Submit: 296
“活着,一定是没有意义的。但是活下去的话,说不定却能遇见有趣的东西。如你遇见这花,如我遇见你。” — 岸本齐史
ccQ毕业前找到了一个好工作,正准备和MM一起庆祝去旅游。但是MM还要求在到目的地之前,一定要先去逛一些她向往已久的城市。这时ccQ就烦恼了,不同城市之间火车票价不同,ccQ想知道如何设计安排合理的线路能顺利地到达目的地,并且能使得他们在到达目的地之前至少经过MM想去的所有的那些城市而使得总费用最少?ccQ为了使MM满意,请你帮他解决这个问题。
输入有多组数据
每组数据第一行有4个整数,n,m,s,t(n <= 100,m <= n*(n-1)/2,0 <= s,t < n),分别表示总的城市数,城市间的线路数,ccQ他们出发时所在城市,以及ccQ他们所要到达的目的地城市。
接下来m行,每行有三个整数a,b,c,(0 <= a,b < n, c < 70000 )代表城市a和城市b之间有一条双向的铁路,票价为c。
有一个整数k(0 <= k <= 16),代表MM想去的城市数,
接下来k个数代表MM想去的城市。(这些城市不与起始城市与目的地城市重复)
如果ccQ找不到合理的线路那么输出”ccQ is not a lucky boy!”(不包含引号) 否则输出能满足MM要求的线路的最小费用。
4 4 0 30 1 21 2 50 2 100 3 221 24 4 0 30 1 21 2 50 2 100 3 204 3 0 30 1 21 2 50 2 1021 2
162ccQ is not a lucky boy!
FOJ有奖月赛-2009年10月——稚鹰翱翔
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 100 ;
const int maxS=262144;
int n,m,S,E,rn;
int x,y,K;
int dis[maxn+3][maxn+3],dp[maxS][18];
int ed;
bool vis[maxn+3];
int id[maxn+3],rid[maxn+3];
void floyd()
{
for0(i,n) dis[i][i]=0;
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
if(dis[i][k]==INF) continue;
for(int j=0;j<n;j++)
{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
void update(int &x,int v)
{
if(v<x) x=v;
}
void TSP()
{
memset(dp,0x3f,sizeof dp);
dp[ysk(id[S])][id[S]]=0;
for(int s=0;s<ed;s++)
{
for(int x=0;x<rn;x++) if(s&ysk(x) &&dp[s][x]!=INF)
{
for(int y=0;y<rn;y++) if(!(s&ysk(y) ))
{
int s2=s|ysk(y);
update(dp[s2][y] ,dp[s][x]+dis[rid[x]][rid[y] ] );
}
}
}
int ans=INF;
for(int x=0;x<rn;x++) if(dp[ed][x]!=INF)
{
ans=min(ans, dp[ed][x]+dis[rid[x]][E ] );
}
if(ans==INF)
{
puts("ccQ is not a lucky boy!");
}
else
{
printf("%d\n",ans);
}
}
int main()
{
std::ios::sync_with_stdio(false);
while(cin>>n>>m>>S>>E)
{
memset(dis,0x3f,sizeof dis);
int x,y,w;
for1(i,m)
{
cin>>x>>y>>w;
dis[x][y]=dis[y][x]=min(dis[x][y],w);
}
cin>>K;
memset(vis,0,sizeof vis);
for1(i,K)
{
cin>>x;
vis[x]=1;
}
int N=0;
id[S]=N;
rid
=S;
N++;
for0(i,n) if(vis[i])
{
id[i]=N;
rid
=i;
N++;
}
rn=N;
ed=ysk(rn)-1;
for0(i,n) if(!vis[i]&&i!=S)
{
id[i]=N++;
}
floyd();
TSP();
}
return 0;
}
Accept: 76 Submit: 296
Time Limit: 2000 mSec Memory Limit : 32768 KB
Problem Description
“活着,一定是没有意义的。但是活下去的话,说不定却能遇见有趣的东西。如你遇见这花,如我遇见你。” — 岸本齐史ccQ毕业前找到了一个好工作,正准备和MM一起庆祝去旅游。但是MM还要求在到目的地之前,一定要先去逛一些她向往已久的城市。这时ccQ就烦恼了,不同城市之间火车票价不同,ccQ想知道如何设计安排合理的线路能顺利地到达目的地,并且能使得他们在到达目的地之前至少经过MM想去的所有的那些城市而使得总费用最少?ccQ为了使MM满意,请你帮他解决这个问题。
Input
输入有多组数据每组数据第一行有4个整数,n,m,s,t(n <= 100,m <= n*(n-1)/2,0 <= s,t < n),分别表示总的城市数,城市间的线路数,ccQ他们出发时所在城市,以及ccQ他们所要到达的目的地城市。
接下来m行,每行有三个整数a,b,c,(0 <= a,b < n, c < 70000 )代表城市a和城市b之间有一条双向的铁路,票价为c。
有一个整数k(0 <= k <= 16),代表MM想去的城市数,
接下来k个数代表MM想去的城市。(这些城市不与起始城市与目的地城市重复)
Output
如果ccQ找不到合理的线路那么输出”ccQ is not a lucky boy!”(不包含引号) 否则输出能满足MM要求的线路的最小费用。
Sample Input
4 4 0 30 1 21 2 50 2 100 3 221 24 4 0 30 1 21 2 50 2 100 3 204 3 0 30 1 21 2 50 2 1021 2
Sample Output
162ccQ is not a lucky boy!
Source
FOJ有奖月赛-2009年10月——稚鹰翱翔 #include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 100 ;
const int maxS=262144;
int n,m,S,E,rn;
int x,y,K;
int dis[maxn+3][maxn+3],dp[maxS][18];
int ed;
bool vis[maxn+3];
int id[maxn+3],rid[maxn+3];
void floyd()
{
for0(i,n) dis[i][i]=0;
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
if(dis[i][k]==INF) continue;
for(int j=0;j<n;j++)
{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
void update(int &x,int v)
{
if(v<x) x=v;
}
void TSP()
{
memset(dp,0x3f,sizeof dp);
dp[ysk(id[S])][id[S]]=0;
for(int s=0;s<ed;s++)
{
for(int x=0;x<rn;x++) if(s&ysk(x) &&dp[s][x]!=INF)
{
for(int y=0;y<rn;y++) if(!(s&ysk(y) ))
{
int s2=s|ysk(y);
update(dp[s2][y] ,dp[s][x]+dis[rid[x]][rid[y] ] );
}
}
}
int ans=INF;
for(int x=0;x<rn;x++) if(dp[ed][x]!=INF)
{
ans=min(ans, dp[ed][x]+dis[rid[x]][E ] );
}
if(ans==INF)
{
puts("ccQ is not a lucky boy!");
}
else
{
printf("%d\n",ans);
}
}
int main()
{
std::ios::sync_with_stdio(false);
while(cin>>n>>m>>S>>E)
{
memset(dis,0x3f,sizeof dis);
int x,y,w;
for1(i,m)
{
cin>>x>>y>>w;
dis[x][y]=dis[y][x]=min(dis[x][y],w);
}
cin>>K;
memset(vis,0,sizeof vis);
for1(i,K)
{
cin>>x;
vis[x]=1;
}
int N=0;
id[S]=N;
rid
=S;
N++;
for0(i,n) if(vis[i])
{
id[i]=N;
rid
=i;
N++;
}
rn=N;
ed=ysk(rn)-1;
for0(i,n) if(!vis[i]&&i!=S)
{
id[i]=N++;
}
floyd();
TSP();
}
return 0;
}
相关文章推荐
- 【POJ】1573 & 【HDU】1035 - Robot Motion(模拟)
- javascript常用系统对象用法
- viewpager轮播图片设置OntounchListener
- Community 2015 安装无法继续因为某些必需的组件失败
- mac 启动tomcat:
- 讯飞语音SDK提示用户校验失败10407
- ASCII码表和常见键盘码
- win10 开启telnet服务
- Rails--%w用法[转]
- 编包----rpm和deb和ISO目录制作出ISO
- Web 服务编程,REST 与 SOAP
- PHP 模拟登录
- JVM常见监控命令使用
- PHPExcel解决内存占用过大问题-设置单元格对象缓存
- Android应用层View绘制流程与源码分析
- jQuery.extend 函数详解
- iOS 10即将来袭!升级你的iOS开发装备
- Android TextView使用HTML处理字体样式、显示图片等
- 16 个 Linux 服务器监控命令和watch
- poj 3678 Labeling Balls