时间限制:1秒
空间限制:65536K
热度指数:613
算法知识视频讲解
题目描述
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.
示例1
输入
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
图论,找两个派的城市。用dijastra算法计算出两个团里面其他店离起点或终点最近的距离。
//#include "stdafx.h"
#include"stdio.h"
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=650;
int n,m;
int u,v,c;
vector<int>vec[2];//分别记录两个团里面的点
int G[maxn][maxn];
int a;
int d[maxn];
int vis[maxn]={0};
const int inf=1000000000;
void dij(int s,int f ){//f=0时计算第一个团里面的其他点到1的最短距离,f=1时计算第二个团里面的其他点到2的最短距离
d[s]=0;
for(int i=0;i<vec[f].size();i++)
{int min=inf,u=-1;
for(int j=0;j<vec[f].size();j++){
int v=vec[f][j];
if(!vis[v]&&min>d[v]){
min=d[v];
u=v;
}}
if(u==-1)return;
vis[u]=1;
for(int k=0;k<vec[f].size();k++)
{int v=vec[f][k];
int w=G[u][v];
if(w!=inf&&!vis[v]&&d[u]+w<d[v]){
d[v]=d[u]+w;
}
}
}
}
int main(){
// freopen("c://jin.txt","r",stdin);
while(cin>>n){
fill(G[0],G[0]+maxn*maxn,inf);
vec[0].clear();//注意每次新输入一组数据保证把原来的两个团清空
vec[1].clear();
cin>>m;
while(m--){
cin>>u>>v>>c;
if(G[u][v]>c){
G[u][v]=c;
G[v][u]=c;
}}
for(int i=1;i<=n;i++)
{cin>>a;
if(a==1)vec[0].push_back(i);
else vec[1].push_back(i);
}
fill(d,d+maxn,inf);
memset(vis,0,sizeof(vis));
dij(1,0);
dij(2,1);
int ans=inf;
for(int i=0;i<vec[0].size();i++){
int u=vec[0][i];
for(int j=0;j<vec[1].size();j++)
{
int v=vec[1][j];
if(G[u][v]!=inf&&ans>d[u]+d[v]+G[u][v])//选择u,v为过渡节点,保证只有u-v一条边连接两个团
{
ans=d[u]+d[v]+G[u][v];//距离为三部分加和
}
}}
if(ans!=inf)
cout<<ans<<endl;
else cout<<"-1"<<endl;
}
// freopen("CON","r",stdin);
// system("pause");
return 0;
}