poj 3767 I Wanna Go Home
题意:n个点(从1-n编号)
m条边
下面m行 u v dis 表示双向边u v的距离
n个点表示 每个点被势力1或2占据
这里保证1 城市由势力1占据,2城市由势力2占据
思路:
求2遍spfa()
从1城市开始求所有 走到所有都是势力1的城市的距离,存在d[1]数组中,d[1][ i ] 表示经过 i 城市任意都是势力1占领的城市的点能到达 1点的最短距离
再由2城市跑一遍spfa ,求出 2势力占领的 距离2 城市最近的距离
最后ans=Min(ans, d[ 1 ] [ i ] + d[ 2 ] [ j ] + dis[ i ][ j ] )
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define N 605
#define INF 99999999 using namespace std; struct node{
int to,nex;
}edge[20005]; int edgenum,head[N]; int dis[N][N],n,ans;
int zhen[N],d[3][N];
bool inq[N];
inline int Min(int a,int b){return a>b?b:a;}
void addedge(int u,int v){
edge[edgenum].to=v;
edge[edgenum].nex=head[u];
head[u]=edgenum++;
}
void spfa(int x,int other){
for(int i=1;i<=n;i++)d[x][i]=INF;
queue<int>q;while(!q.empty())q.pop();
memset(inq,0,sizeof(inq)); d[x][x]=0;
q.push(x); inq[x]=1; while(!q.empty())
{
int u=q.front();q.pop(); inq[u]=0;
for(int i=head[u];i!=-1;i=edge[i].nex)
{
int v=edge[i].to;
if(zhen[u]==zhen[v] && d[x][v]>d[x][u]+dis[u][v] )
{
d[x][v]=d[x][u]+dis[u][v];
if(!inq[v] && v!=other)
inq[v]=true, q.push(v);
}
}
}
} int main(){
int i,m;
while(scanf("%d",&n),n)
{
scanf("%d",&m);
memset(head,-1,sizeof(head)); memset(dis,-1,sizeof(dis));
edgenum=0; while(m--){
int u,v,dd;
scanf("%d%d%d",&u,&v,&dd);
if(dis[u][v]==-1)//去重边
addedge(u,v),addedge(v,u),dis[u][v]=dis[v][u]=dd;
else dis[u][v]=dis[v][u]=Min(dis[u][v],dd);
}
for(i=1;i<=n;i++)scanf("%d",&zhen[i]); ans=INF;
spfa(1,2);
spfa(2,1);
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(zhen[i]!=zhen[j] && dis[i][j]!=-1)
ans=Min(ans,d[1][i]+d[2][j]+dis[i][j]); if(ans>INF/2)ans=-1;
printf("%d\n",ans);
}
return 0;
}
poj 3767 I Wanna Go Home的更多相关文章
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
随机推荐
- 五毛的cocos2d-x学习笔记05-场景与场景动画,动作
场景切换函数: Director->getInstance()->replaceScene(Scene*); Director->getInstance()->runWithS ...
- jz2440不能成功地启动文件系统, Failed to execute /linuxrc.
文件系统加载失败,错误信息提示: VFS: Mounted root (nfs filesystem). Freeing init memory: 140K Failed to ex ...
- 在VS2010上使用C#调用非托管C++生成的DLL文件
背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用.本篇文章将引导你快速理解这个调用 ...
- 【转】从底层了解ASP.NET体系结构
从底层了解ASP.NET体系结构 原文:http://blog.csdn.net/zhoufoxcn/article/details/1890158 Java体系架构的书多如牛毛,比如SSH架构什么的 ...
- UIView 设置阴影(属性说明)
以下代码实现: 第一个图片的代码 //加阴影--任海丽编辑 _imageView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowCo ...
- Dapper 多数据库优化
Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...
- 【转】页面尺寸不一样的PDF页面调整方法
本文综合参考:http://www.360doc.com/content/10/1114/22/2961363_69395272.shtml http://blog.sina.com.cn/s/blo ...
- 开启Linux VNC远程桌面
Xwindows:gnome (红帽默认安装的图形界面) 一, 确认及安装VNCSERVER. 1,首先确认你服务器是否配置了VNCSERVER,可以在命令行下敲入以下命令查看: [root@lo ...
- hdu4405概率dp入门
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- STL之如何选择顺序容器
一.顺序容器的分类 顺序容器:vector向量.list链表.deque双端队列: 优先级最高的是vector向量,它的速度比较快,优点最多: 在程序设计中,容器可以切换: #include < ...