题目描述
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.
/*N 城市数量 【2,600】
M 道路数 【0,10000】
M行 道路信息 A地到B地 时长T【1,500】
N个整数 第一个整数显示了第一城市的支持领导
注:路是双向的,两个城市最多有一条路,输入以N=0为结束
输出到达家的最短时间,否则输出-1*/
#include<stdio.h>
#include<limits.h>
#define N 601
int G[N][N];//城市间的距离,用时间表示
int camp[N];//各个城市的阵营
int n[3];//n[0]:城市数量;n[1]、n[2]:两城市各自的阵营数量
int Add(int a,int b)
{
//两个数相加
if(a==INT_MAX||b==INT_MAX) return INT_MAX;
else return a+b;
}
int Add3(int a,int b,int c)
{
if(a==INT_MAX||b==INT_MAX||c==INT_MAX) return INT_MAX;
else return a+b+c;
}
int d1[N],d2[N];//分别存储1,2到同阵营各点最小距离
bool mark[N];//标记点是否在集合中
void Dijkstra(int d[N],int from)
{
//求某点到同阵营各点的最小距离
for(int i=0;i<=n[0];i++)
{
//不同阵营的先塞进集合里
if(camp[i]==from) mark[i]=false;
else mark[i]=true;
}
mark[from]=true;//把起点塞进集合
for(int i=0;i<=n[0];i++)//初始化d[N]
{
if(camp[i]==from) d[i]=G[from][i];
}
for(int i=0;i<n[camp[from]]-1;i++)//把同阵营的点全都塞进集合
{
bool isFirst=true;
int near;
for(int i=1;i<=n[0];i++)//挑一个[?]最小的,下标为near
{
if(mark[i]==false)
{
if(isFirst)
{
near=i;
isFirst=false;
}
else if(d[i]<d[near]) near=i;
}
}
mark[near]=true;//加入集合
for(int i=1;i<=n[0];i++)//更新同阵营点的的d[i]信息
{
if(camp[i]==from&&mark[i]==false)
{
int sum=Add(d[near],G[near][i]);
if(sum<d[i]) d[i]=sum;
}
}
}
}
int main()
{
int m;
while(scanf("%d",&n[0])!=EOF)
{
if(n[0]==0) break;
scanf("%d",&m);
for(int i=0;i<=n[0];i++)//初始化邻接矩阵
{
for(int j=0;j<=n[0];j++)
{
if(i==j) G[i][j]=0;
else G[i][j]=INT_MAX;
}
}
while(m--)//读取边
{
int a,b,t;
scanf("%d%d%d",&a,&b,&t);
if(t<G[a][b]) {G[a][b]=G[b][a]=t;}
}
n[1]=n[2]=0;
for(int i=1;i<=n[0];i++)
{
scanf("%d",&camp[i]);
n[camp[i]]++;
}
Dijkstra(d1,1);//求出1到同阵营各点的最小距离
Dijkstra(d2,2);//求出2到同阵营各点的最小距离
int ans=INT_MAX;//先假设最小距离是无穷大
for(int i=1;i<=n[0];i++)
{
for(int j=i+1;j<=n[0];j++)//i是阵营1j是阵营2的话
{//看看从这条路过境是否更划算
if(camp[i]==1&&camp[j]==2)
{
int sum=Add3(d1[i],d2[j],G[i][j]);
if(sum<ans) ans=sum;
}
else if(camp[i]==2&&camp[j]==1)
{
int sum=Add3(d2[i],d1[j],G[i][j]);
if(sum<ans) ans=sum;
}
}
}
if(ans!=INT_MAX) printf("%d",ans);
else printf("-1\n");
}
return 0;
}