-
题目描述:
-
The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.
"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
Would you please tell Mr. M at least how long will it take to reach his sweet home?
-
输入:
-
The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
-
输出:
-
For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M's demands, output -1 instead.
-
样例输入:
-
2 1 1 2 100 1 2 3 3 1 2 100 1 3 40 2 3 50 1 2 1 5 5 3 1 200 5 3 150 2 5 160 4 3 170 4 2 170 1 2 2 2 1 0
-
样例输出:
-
100 90 540
只需要确保连接1阵营和2阵营城市之间的道路是从1-> 2单向的就好了。这样求解最短路时只要到达2阵营就无法返回1阵营了。
具体代码实现:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
struct Edge
{
int u,v;
int d;
Edge(int tu,int tv,int td):u(tu),v(tv),d(td){}
};
struct HeadNode
{
int u;
int d;
HeadNode(int tu,int td):u(tu),d(td){}
bool operator < (const HeadNode& rhs) const
{
return d>rhs.d;
}
};
const int maxn=600+5;
struct Dij
{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
void init(int tn)
{
n=tn;
edges.clear();
for(int i=1;i<=n;++i)
G[i].clear();
}
void addEdge(int u,int v,int d)
{
edges.push_back(Edge(u,v,d));
m=edges.size();
G[u].push_back(m-1);
}
void dijkstra(int s)
{
fill(done,done+maxn,false);
fill(d,d+maxn,INT_MAX);
priority_queue<HeadNode> pq;
pq.push(HeadNode(s,0));
d[s]=0;
while(!pq.empty())
{
HeadNode t=pq.top();pq.pop();
int u=t.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();++i)
{
Edge e=edges[G[u][i]];
if(d[e.v]>d[u]+e.d)
{
d[e.v]=d[u]+e.d;
pq.push(HeadNode(e.v,d[e.v]));
}
}
}
}
};
Dij dij;
int p[maxn];
const int maxm=10000+4;
int a[maxm];
int b[maxm];
int c[maxm];
int main()
{
int n;
for(cin>>n;n!=0;cin>>n)
{
dij.init(n);
int m;
cin>>m;
for(int i=0;i<m;++i)
{
int u,v,d;
cin>>u>>v>>d;
a[i]=u;
b[i]=v;
c[i]=d;
}
for(int i=1;i<=n;++i)
{
int t;
cin>>t;
p[i]=t;
}
for(int i=0;i<m;++i)
{
int u=a[i],v=b[i],d=c[i];
if(p[u]!=1)
{
int temp=u;
u=v;
v=temp;
}
dij.addEdge(u,v,d);
if(p[u]==p[v])
dij.addEdge(v,u,d);
}
dij.dijkstra(1);
if(dij.d[2]<INT_MAX)
cout<<dij.d[2]<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}