PKU I Wanna Go Home 3767 Dijsktra 优先队列 + BFS

来源:互联网 发布:javascript编程视频 编辑:程序博客网 时间:2024/05/19 21:15

/*
I Wanna Go Home
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 1134  Accepted: 468

Description

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?

Input

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.

Output

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.

Sample Input

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

Sample Output

100
90
540

Source
*/
//这是一次月赛的题目,那是题目没有看懂,结果当然没有做出来,今天终于,继续最短短
//那次比赛后zhc说可以用bfs,等下试试
#include<iostream>//7096486 huixisheng 3767 Accepted 700K 32MS C++ 1220B 2010-06-29 14:16:50
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;

int cost[605][605];
int hash[605];
bool Hash[605];
int dis[605];
int n;
const int MAX1 = 0x7fffffff;//这里用7个f,16ms的,奇怪

void Dijkstra()
{
 int i, j, k;
 int t;
 memset(Hash, false, sizeof(Hash));
 Hash[1] = true;
 for(i = 2; i <= n; i++)
 {
  int d = MAX1;
  t = 1;//  这里忘记了初始话,一直错 RE都有,太崩溃了。
  for(j = 2; j <= n; j++)
  {
   if(!Hash[j] && dis[j] < d)
   {
    t = j;
    d = dis[j];
   }
  }
  for(k = 2; k <= n; k++)
  {
   if(!Hash[k] && cost[t][k] != MAX1 && dis[k] > dis[t] + cost[t][k] && !( hash[t] == 2 && hash[k] == 1 ))
    dis[k] = dis[t] + cost[t][k];
  }
  Hash[t] = true;
 }
 if(dis[2] != MAX1)
  cout<<dis[2]<<endl;
 else
  cout<<"-1"<<endl;

}
int main()
{
 int m, a, b, t, i, j;
 while(scanf("%d", &n) != EOF && n)
 {
  scanf("%d", &m);
  for(i = 1; i <= n; i++)
  for(j = 1; j <= n; j++)
  {
   if(i == j)
    cost[i][j] = 0;
   else
    cost[i][j] = MAX1;
  }
  for(i = 0; i < m; i++)
  {
   scanf("%d %d %d", &a, &b, &t);
   cost[a][b] = t;
   cost[b][a] = t;
  }
  for(i = 1; i <= n; i++)
  {
   dis[i] = cost[1][i];
   scanf("%d", &hash[i]);
  }
  Dijkstra();
  
 }
 return 0;
}

 

/*
 搞了很长的时间,终于AC。看来对优先队列不熟悉
*/
#include<iostream>//7097801 huixisheng 3767 Accepted 708K 16MS C++ 1797B 2010-06-29 21:16:29
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

int cost[605][605];
int dis[605];
int flag[605];
bool hash[605];
const int MAX = 0x7fffffff;
int n, df, maxs;
 struct node
 {
  int num;
  int flag1;
  int dist;
  friend bool operator < (node a, node b)
 {
   return a.dist > b.dist;
 }
 };
void BFS()
{
 priority_queue<node> Q;
 memset(hash, false, sizeof(hash));
 node P, N;
 /*
 P.dist = dis[df];
 P.flag1 = flag[df];
 P.num = df;
 hash[1] = true;
 */
 for(int i = 2; i <= n; i++)//跟1相邻的点要全部进队列
 {
  if(dis[i] != MAX)
  {
   P.dist = dis[i];
   P.flag1 = flag[i];
   P.num = i;
   Q.push(P);
  }
 }
 //Q.push(P);
 while(!Q.empty())
 {
  N = Q.top();
  Q.pop();
  hash[N.num] = true;
  for(int i = 1; i <= n; i++)
  {
   if(!hash[i] && cost[N.num][i] != MAX && N.dist + cost[N.num][i] < dis[i] && !(N.flag1 == 2 && flag[i] == 1) )
   {
    dis[i] = dis[N.num] + cost[N.num][i];
    P.dist = dis[i];
    P.num = i;
    P.flag1 = flag[i];
    Q.push(P);
    if(hash[2])
    {
     cout<<dis[2]<<endl;
     return ;
     //break;
    }
   }
   
  }
 }
 if(dis[2] != MAX)
  cout<<dis[2]<<endl;
 else
  cout<<"-1"<<endl;
}
int main()
{
 int m, a, b, t, i, j;
  while(scanf("%d", &n) != EOF && n)
   {
  scanf("%d", &m);
  for(i = 1; i <= n; i++)
  for(j = 1; j <= n; j++)
  {
   //if(i == j)
   //cost[i][j] = 0;
   //else
   cost[i][j] = MAX;
  }
  
   for(i = 0; i < m; i++)
  {
    scanf("%d %d %d", &a, &b, &t);
    cost[a][b] = t;
    cost[b][a] = t;
  }
  //df = 1;
  //maxs = MAX;
  for(i = 1; i <= n; i++)
  {
   dis[i] = cost[1][i];
   /*
   if(maxs > dis[i])
   {
    maxs = dis[i];
    df = i;
   }
   */
   scanf("%d", &flag[i]);
  }
  BFS();
  }
}

原创粉丝点击