hdu 2066 一个人的旅行(超级纠结)终于AC (用邻接表写了一个)

来源:互联网 发布:java点赞功能实现 编辑:程序博客网 时间:2024/05/20 12:51

/*

 
 这是学了Dijsk 最初写的代码,一开始一直错。就是找不到原因
 连小wc也没有看出,t应该研究了好久,终于让我意识到自己写代码是多么粗心
 值得收藏的代码,当做一个惨痛的教训
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAX = 0x7ffffff;
const int Ty = 1005;
int cost[Ty][Ty];
int dis[Ty];
bool hash[Ty];
int n, dd[Ty], d;
/*
void Floyd(int n)
{
    int u,v,w;
for(v=1; v <= n; v++)
        for(w=1; w <= n; w++)
            df[v][w] = cost[v][w];
        for(u=1; u<= n; u++)
        for(v=1; v<= n; v++)
        for(w=1; w<= n; w++)
           if(df[v][u] + df[u][w] < df[v][w])
                df[v][w] = df[v][u] + df[u][w];
}

*/
int  Dijkstra(int start, int n)
{
// printf("%d/n", n);
int i, j, k;
int max1 = MAX;//这里写的不好,让人看不懂,定义变量的时候要注意,选有代表性的
for(i = 1; i <= n; i++)
{
dis[i] = cost[start][i];
}
memset(hash, false, sizeof(hash));
hash[start] = true;
for(i = 1; i < n; i++)
{
int d = MAX;
int t = start;
for(j = 1; j <= n; j++)//纠结了好久的题目,问题应该在这里吧。。
{
if(!hash[j] && dis[j] < d)
{
d = dis[j];
t = j;
}
}
for(k = 1; k <= n; k++)
{
if(!hash[k] && cost[t][k] != MAX && dis[k] > dis[t] + cost[t][k])
dis[k] = dis[t] + cost[t][k];
}
hash[t] = true;
}
//return dis[end];
/*
for(i = 0; i < d; i++)
{
if(dis[dd[i]] < max1 )
max1 = dis[dd[i]];
}
*/
for(i = 0; i < d; i++)
{
if(dd[i] && dis[dd[i]] < max1)
max1 = dis[dd[i]];
}
return max1;

}

int main()
{
int t, s, i, j;
int a, b, val;
int ss[Ty];
int MAX_sum;
while(scanf("%d %d %d", &t, &s, &d) != EOF)
{
n = 0;
for(i = 0; i < Ty; i++)
for(j = 0; j < Ty; j++)
{
if(i == j)
cost[i][j] = 0;
else
cost[i][j] = MAX;
//dd[i] = 0;
}
for(i = 0; i < t; i++)
{
scanf("%d%d%d", &a, &b, &val);
if(val < cost[a][b])
{
cost[a][b] = val;
cost[b][a] = val;
}
if(a > n)
n = a;
if(b > n)
n = b;
}
for(i = 0; i < s; i++)
scanf("%d", &ss[i]);
for(i = 0; i < d; i++)
scanf("%d", &dd[i]);
MAX_sum = MAX;
//Floyd(n);
//n = 1002;
for(i = 0; i < s; i++)
{
 int ttt = Dijkstra(ss[i], n);
if(MAX_sum > ttt)
MAX_sum = ttt;

//if(MAX_sum > df[ss[i]][dd[j]] )//这里浪费时间
//MAX_sum = df[ss[i]][dd[j]];
}
printf("%d/n", MAX_sum);
}
}

 

 

/*
 把原点看成看为0,可以走到的点dis[] = 0;
 超酷的加点(zhc提供的方法)
*/
#include<iostream>//00444199 2010-06-30 09:58:01 Accepted 1002 46 MS 4196 KB Visual C++ 悔惜晟 
#include<cstring>
#include<cstdio>
using namespace std;

const int N = 1005;
const int MAX = 0x3fffffff;
int dis[N];
int cost[N][N];
bool hash[N];

void  Dijsktra(int n)
{
 int i, j, k;
 int d;
 for(i = 1; i <= n ; i++)
 {
  dis[i] = cost[0][i];
  //printf("%d/n", dis[i]);
 }
 //printf("%d--/n", n);
 memset(hash, false, sizeof(hash));
 hash[0] = true;
 dis[0] = 0;
 
 for(i = 1; i <= n; i++)
 {
  d = MAX;
  int t;
  for(j = 1; j <= n; j++)
  {
   if(!hash[j] && dis[j] < d)
   {
    d = dis[j];// 我的天啊 找了好久原来是这里错了
    t = j;
   }
   
  }
  hash[t] = true;
  for(k = 1; k <= n; k++)
  {
   if(!hash[k] && dis[k] > dis[t] + cost[t][k])
    dis[k] = dis[t] + cost[t][k];
  }
  //printf("%d/n", dis[t]);
 }
}
int main()
{
 int t, s, d;
 int a, b, val;
 int i, j;
 int ss, dd;
 while(scanf("%d %d %d", &t, &s, &d) != EOF)
 {
  for(i = 0; i < N; i++)
  for(j = 0; j < N; j++)
  {
   if(i == j)
    cost[i][j] = 0;
   else
    cost[i][j] = MAX;
  }
  int max = 0;
  for(i = 0; i < t; i++)
  {
   scanf("%d %d %d", &a, &b, &val);
   if(a > max)
    max = a;
   if(b > max)
    max = b;
   if(val < cost[a][b] )
   {
    cost[a][b] = val;
    cost[b][a] = val;
   }
  }
  for(i = 0; i < s; i++)
  {
   scanf("%d", &ss);
   cost[0][ss] = 0;
   cost[ss][0] = 0;
   dis[ss] = 0;
  }
  Dijsktra(max);
  int ans = MAX;
  for(i = 0; i < d; i++)
  {
   scanf("%d", &dd);
   //printf("%d---/n", dis[dd]);
   if (ans > dis[dd])
   {
    ans = dis[dd];
   }
  }
  printf("%d/n", ans);
 }
}