一、题目大意
二、解题思路
两个阵营,只能越过一次阵营,因此把跨阵营的路建模为单向边即可
三、代码
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int MAX = 606;
struct Node
{
int v;
int t;
bool operator <(const Node&A)const
{
return t > A.t;
}
};
priority_queue<Node> Q;
vector<Node> G[MAX];
int d[MAX];
int N, M;
int id2team[MAX];
const int inf = 1 << 29;
void djstla()
{
for(int i=0; i<N; i++)
d[i] = inf;
d[0] = 0;
while(!Q.empty())
Q.pop();
Q.push((Node){0,0});
while(!Q.empty())
{
Node top = Q.top(); Q.pop();
if(top.v == 1)
return;
for(int idv=0; idv<G[top.v].size(); idv++)
{
int n_v = G[top.v][idv].v;
int n_t = top.t + G[top.v][idv].t;
if(n_t >= d[n_v])
continue;
d[n_v] = n_t;
Q.push((Node){n_v, n_t});
}
}
}
int main()
{
while(cin >> N)
{
if(N == 0)
break;
for(int i=0; i<M; i++)
G[i].clear();
cin >> M;
int s, e, t;
for(int i=0; i<M; i++)
{
cin >> s >> e >> t;
s = s-1;
e = e-1;
G[s].push_back((Node){e, t});
G[e].push_back((Node){s, t});
}
for(int i=0; i<N; i++)
cin >> id2team[i];
for(int v=0; v<N; v++)
{
for(int idv=0; idv<G[v].size(); idv++)
{
int next_v = G[v][idv].v;
if(id2team[v] != id2team[next_v] && id2team[v] == 2)
G[v][idv].t = inf;
}
}
djstla();
if(d[1] == inf)
cout << -1 << endl;
else
cout << d[1] << endl;
}
return 0;
}