m条边

n个点表示 每个点被势力1或2占据

#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 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;}
edge[edgenum].to=v;
}
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;
{
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);
edgenum=0;

while(m--){
int u,v,dd;
scanf("%d%d%d",&u,&v,&dd);
if(dis[u][v]==-1)//去重边
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的更多相关文章

1. poj 1236 Network of Schools(又是强连通分量+缩点)

http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

4. POJ 2965. The Pilots Brothers&#39; refrigerator 枚举or爆搜or分治

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

5. POJ 1753. Flip Game 枚举or爆搜+位压缩，或者高斯消元法

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

6. POJ 3254. Corn Fields 状态压缩DP (入门级)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

7. POJ 2739. Sum of Consecutive Prime Numbers

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

8. POJ 2255. Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

9. 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 ...

## 随机推荐

1. 【BZOJ 1005】【HNOI 2008】明明的烦恼

http://www.lydsy.com/JudgeOnline/problem.php?id=1005 答案是\[\frac{(n-2)!}{(n-2-sum)!×\prod_{i=1}^{cnt} ...

2. mysql errno 150

mysql error Number 1005can't creat table'/test/#sql-640_1.frm'(errno:150)三种可能问题 外键和被引用外键类型不一样,比如inte ...

3. 混合使用C和C++

C++作为C语言的扩展集,几乎所有的C程序都可以在C++中编译和运行,但是要注意C程序中可能使用了C++中的关键字作为变量,比如在C中:int class = 0; 但这在C++中不行.出于方便性,我 ...

4. c++ 的几种强制转换的讨论

http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html static_cast  : 同 c 的强制转换类似: rei ...

5. HDU 5879 Cure

Cure Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

6. Windows 系统消息范围和前缀，以及消息大全

Windows系统定义的消息类别消息标识符前缀 消息分类ABM 应用桌面工具栏消息BM 按钮控件消息CB 组合框控件消息CBEM 扩展组合框控件消息CDM 通用对话框消息DBT 设备消息DL 拖曳列表 ...

7. 【转】Uiautomator Api浅析

原文地址:http://blog.sina.com.cn/s/blog_ae2575ff01018b2o.html uiautomator api: http://android.toolib.net ...

8. Kali Linux之使用SET快捷生成钓鱼网站方法

SET (Social Engineering Tools) 1.使用命令:setoolkit 会显示工具菜单 2.输入1 ,选择菜单中的Social-Engineering Attacks (社会工 ...

9. 【转】shell速查表

shell速查表 转自:https://www.jianshu.com/p/a98a334bfb25 1. 变量 #!/bin/bash msg="hello world" ech ...

10. 【计算机视觉】seetaFace

class impl class FaceDetection::Impl { public: Impl() : detector_(new seeta::fd::FuStDetector()), sl ...