题目:hdoj 2066 一个人的旅行

方法:缩点 + 最短路

分析:看了大神的一篇博客,讲冗余压缩的,然后就想找一个多源最短路练练手。

这个题目就是典型的多源多汇最短路

方法:把全部的源点压缩成一个点,然后汇点压缩成一个点,然后跑最短路

注意:

1:求最短路的时候邻接表存储有重边不影响结果。

2:此题有重边。

3:要特殊处理源点和汇点是同一个点的情况。为0

AC代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1050;
int mp[N][N];
map<int,int> ma;
struct Node
{
int to,val;
};
vector<Node> v[N];
void add_Node(int x,int y,int z)
{
v[x].push_back((Node){y,z});
v[y].push_back((Node){x,z});
} int dis[N];
bool ok[N];
void spfa(int s)
{
queue<int> q;
q.push(s);
memset(dis,inf,sizeof(dis));
dis[s]=0;
while(!q.empty())
{
int tmp=q.front();
q.pop();
for(int i=0;i<v[tmp].size();i++)
{
if(dis[v[tmp][i].to]>dis[tmp]+v[tmp][i].val)
{
dis[v[tmp][i].to]=dis[tmp]+v[tmp][i].val;
q.push(v[tmp][i].to);
}
}
}
} void MP_clear(int n)
{
ma.clear();
for(int i=0;i<=n;i++)
v[i].clear();
}
int main()
{
int t,s,d;
while(~scanf("%d%d%d",&t,&s,&d))
{
memset(mp,inf,sizeof(mp));
int ma_x=0,ma_y=0;
for(int i=0;i<t;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
mp[x][y]=min(z,mp[x][y]);
ma_x=max(ma_x,x);
ma_y=max(ma_y,y);
}
memset(ok,0,sizeof(ok));
int ss=max(ma_x,ma_y)+2,tt=max(ma_x,ma_y)+1;
for(int i=0;i<s;i++)
{
int x;scanf("%d",&x);
ma[x]=ss;
ok[x]=1;
}
int ff=0;
for(int i=0;i<d;i++)
{
int x;
scanf("%d",&x);
ma[x]=tt;
if(ok[x] && ff==0)
ff=1;
}
if(ff==1)
{
printf("0\n");
continue;
}
for(int i=1;i<=ma_x;i++)
{
for(int j=1;j<=ma_y;j++)
{
if(mp[i][j]!=inf)
{
int xx=i,yy=j;
if(ma[i])
xx=ma[i];
if(ma[j])
yy=ma[j];
add_Node(xx,yy,mp[i][j]);
}
}
}
spfa(ss);
printf("%d\n",dis[tt]);
MP_clear(ss);
}
return 0;
}

hdoj 2066 一个人的旅行 【多源多汇最短路】的更多相关文章

  1. Vijos 1006 晴天小猪历险记之Hill 单源单汇最短路

    背景 在很久很久以前,有一个动物村庄,那里是猪的乐园(^_^),村民们勤劳.勇敢.善良.团结-- 不过有一天,最小的小小猪生病了,而这种病是极其罕见的,因此大家都没有储存这种药物.所以晴天小猪自告奋勇 ...

  2. hdoj 2066 一个人的旅行

    Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰 ...

  3. POJ1511 Invitation Cards(多源单汇最短路)

    边取反,从汇点跑单源最短路即可. #include<cstdio> #include<cstring> #include<queue> #include<al ...

  4. 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路

    1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...

  5. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  6. hdu 2066 一个人的旅行

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷 ...

  7. poj1459 Power Network (多源多汇最大流)

    Description A power network consists of nodes (power stations, consumers and dispatchers) connected ...

  8. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

  9. poj2112 二分+floyd+多源多汇最大流

    /*此题不错,大致题意:c头牛去k个机器处喝奶,每个喝奶处最多容纳M头牛,求所有牛中走的最长路的 那头牛,使该最长路最小.思路:最大最小问题,第一灵感:二分答案check之.对于使最长路最短, 用fo ...

随机推荐

  1. 用QT创建WINDOWS服务程序

    恩, qtservice挺好的http://www.qtsoftware.com/products/appdev/add-on-products/catalog/4/Utilities/qtservi ...

  2. Flask web开发 处理POST请求(登录案例)

    本文我们以一个登录例子来说明Flask对 post请求的处理机制. 1.创建应用目录,如 mkdir   example cd example 2.在应用目录下创建  run.py文件,内容如下 fr ...

  3. Linux命令之find(一)

    find命令的使用格式为:find options path expressions find命令事实上有两种options,一种是"真正属于自己的",还有一种位于expressi ...

  4. UVa 121 - Pipe Fitters

    称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...

  5. CF#213DIV2:B The Fibonacci Segment

    You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for ...

  6. 输入输出函数 I/O函数之perror()

    perror()函数的函数原型 void perror(char const *message); 它会将message信息输出出来,后面再加上错误原因字符串. 下面是来自百度百科的实例: #incl ...

  7. 高级UIKit-04(NSUserDefaults、NSKeyedArchiver、对象归档方法)

    [day05_1_UserDefault]:判断应用程序是否是第一次运行 NSUserDefaults:用来保存应用程序的配置信息如:程序运行次数,用户登陆信息等. // 使用系统提供的NSUserD ...

  8. 17.1 Replication Configuration 复制:

    17.1 Replication Configuration 复制: 17.1.1 How to Set Up Replication 17.1.2 Replication Formats 17.1. ...

  9. CCIE路由实验(5) -- BGP负载均衡

    enableconf tno ip do loenable pass ciscoline con 0logg syncexec-t 0 0exitline vty 0 4pass ciscologg ...

  10. Testin_百度百科

    Testin_百度百科     Testin    编辑    目录     1测试平台     2三大特性    #1 真机终端云,节省测试设备购买租赁成本#2 自动化测试,节省测试人员成本及时间# ...