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 ...
随机推荐
- iOS系统原生二维码条形码扫描
本文讲述如何用系统自带的东东实现二维码扫描的功能:点击当前页面的某个按钮,创建扫描VIEW.细心的小伙伴可以发现 title被改变了,返回按钮被隐藏了.这个代码自己写就行了,与本文关系不大...绿色的 ...
- JavaSE学习总结第11天_开发工具 & API常用对象1
11.01 常见开发工具介绍 1:操作系统自带的记事本软件 2:高级记事本软件例:Editplus,Notepad++,UltraEdit 3:集成开发环境 IDE(Integrated Deve ...
- mysql innodb存储引擎的聚集索引
InnoDB聚集索引 MySQL有没有支持聚集索引,取决于采用哪种存储引擎. MySQL InnoDB一定会建立聚集索引,所谓聚集,指实际数据行和相关的键值保存在一块,这也决定了一个表只能有一个聚集索 ...
- hibernate 数据关联一对多 3.1
一对多,多对一 (在多的一端存放一的外键) 但是在实体类中不需要创建这个外键 // 在一的一方创建Set集合 public class User { private Integer id; priva ...
- 梳排序(Comb sort)
Comb Sort,梳排序或者梳子排序,就像梳子那样有间隔地比较两个数,很形象,O(n*logn)时间复杂度,O(1)空间复杂度,属于不稳定的排序算法.算法的思想是使逆序的元素尽可能快地移动到最终的位 ...
- python 模块BeautifulSoup使用
BeautifulSoup是一个专门用于解析html/xml的库.官网:http://www.crummy.com/software/BeautifulSoup/ 说明,BS有了4.x的版本了.官方说 ...
- python list comprehension twos for loop 嵌套for循环
list comprehension 后面可以有多个for loops,每个for后面可以有if [(x, y, x * y)for x in(0,1,2,3)for y in(0,1,2,3)if ...
- Shiro入门(1)
=============基本概念=================== 什么是Apache Shiro? Apache Shiro(发音为“shee-roh”,日语“堡垒(Castle)”的意思)是 ...
- android一个弹出菜单的动画(二)
假设做一个弹出的控件,我们能够进行加入view: 写class SatelliteMenu extends FrameLayout private void init(Context context, ...
- 树莓派deian的linux常用命令
Linux系统,这个强大的系统,现在树莓派也要用到.给大家普及一下. 那些常用的Linux命令 linux的文件结构 / 根目录下的目录 /bin /home /dev /usr /opt /et ...