http://ac.jobdu.com/problem.php?pid=1162
#include <stdio.h>
#include <cstring>
const static int INF = 1000000000;
static int dist[606];
static bool visit[606];
static int support[606];
static int G[606][606];
static int n,m;
void dijkstra(int s, int e){
visit[s] = true;
dist[s] = 0;
for (int i = 1; i <=n; ++i) {
if(G[s][i] != INF){
dist[i] = G[s][i];
}
}
for (int i = 1; i <n; ++i) {
int td = INF,tnode = s;
for (int j = 1; j<=n; j++) {
if(!visit[j] && (dist[j] < td)){
td = dist[j];
tnode = j;
}
}
//printf("tnode,td:%d,%d\n",tnode,td);
visit[tnode] = true;
if(tnode == s|| tnode == e) return;
for (int k = 1; k <=n; ++k) {
if(!visit[k] && ( dist[k] > td + G[tnode][k] && !(support[tnode]==2 && support[k] ==1)))
dist[k] = td + G[tnode][k];
}
}
}
int main(){
//freopen("in/1162.in","r",stdin);
int a,b,c;
while(scanf("%d",&n)!=EOF && n!=0 ){
scanf("%d",&m);
memset(visit,false,sizeof(visit) ) ;
for (int i = 0; i <=n; ++i) {
for (int j = 0; j <=n; ++j) {
G[i][j] = G[j][i] = INF;
}
}
for (int i = 0; i < m; ++i) {
scanf("%d%d%d",&a,&b,&c);
G[a][b] = G[b][a] = c;
}
for (int i = 1; i <=n; ++i) {
scanf("%d",&support[i]);
dist[i] = INF;
visit[i] = false;
}
dijkstra(1,2);
if (visit[2]) {
printf("%d\n",dist[2]);
}else{
printf("-1\n");
}
}
}