您的位置:首页 > 其它

hdoj 2680 Choose the best route 【dijkstra】

2015-08-18 19:27 357 查看


<span style="font-family: 'Times New Roman', Times, serif;">Choose the best route</span>


Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)


Total Submission(s) : 61 Accepted Submission(s) : 31


Problem Description

One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take.
You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.

Input

There are several test cases. Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n="" stands="" for="" the="" number="" of="" bus="" stations="" in="" this="" city="" and="" m="" directed="" ways="" between="" .(maybe="" there="" are=""
several="" two="" .)="" s="" station="" that="" near="" kiki’s="" friend’s="" home.="" then="" follow="" lines="" ,each="" line="" contains="" three="" integers="" p="" ,="" q="" t="" (0<t<="1000)." means="" from="" to="" is="" a="" way="" it="" will="" costs=""
minutes="" .="" with="" an="" integer="" w(0<w<n),="" kiki="" can="" take="" at="" beginning.="" follows="" w="" these="" stations.="" <="" div="">

Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.

Sample Input

5 8 5
1 2 2
1 5 3
1 3 4
2 4 7
2 5 6
2 3 5
3 5 1
4 5 1
2
2 3
4 3 4
1 2 3
1 3 4
2 3 2
1
1


Sample Output

1
-1


Author
dandelion
分析:
这道题用常规的dijkstra做不行,要改变一下,wa了我好几次。真是醉了。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define min(x,y) (x<y?x:y)
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,s;
int map[1010][1010];
int dis[1010],vis[1010];
void dij()
{
memset(vis,0,sizeof(vis));
int i,j,k,mins;
vis[0]=1;
for(i=0;i<=n;i++)
dis[i]=map[0][i];
for(i=0;i<=n;i++)
{
mins=INF;
for(j=0;j<=n;j++)
if(!vis[j]&&dis[j]<mins)
mins=dis[k=j];
vis[k]=1;
for(j=0;j<=n;j++)
if(!vis[j])
dis[j]=min(dis[j],map[k][j]+dis[k]);
}

}

int main()
{
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
int a,b,c;
memset(map,INF,sizeof(map));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=c;
}
int t;
scanf("%d",&t);
while(t--)
{
int x;
scanf("%d",&x);
map[0][x]=0;
}
dij();
if(dis[s]==INF)
printf("-1\n");
else
printf("%d\n",dis[s]);
}
return 0;
}


错误代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define min(x,y) (x<y?x:y)
using namespace std;
int n,m,s;
const int INF=0x3f3f3f3f;
int map[1010][1010];
int vis[1010], dis[1010];

void dij()
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[0]=0;
while(true)
{
int v=-1;
for(int i=0;i<=n;i++)
if(!vis[i]&&(v==-1||dis[v]>dis[i]))
v=i;
if(v==-1)
break;
vis[v]=1;
for(int j=0;j<=n;j++)
dis[j]=min(dis[j],dis[v]+map[v][j]);
}
if(dis[s]==INF)
printf("-1\n");
else
printf("%d\n",dis[s]);
}

int main()
{
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
int a,b,c,x,f;
memset(map,INF,sizeof(map));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])
map[a][b]=map[b][a]=c;
}
scanf("%d",&f);
while(f--)
{
scanf("%d",&x);
map[0][x]=0;
}
dij();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: