[HDU2680] Choose the best route [单源最短路]

反向建图或者超级源点

dijkstra 不知道wa在哪里 就很恐怖了
UPD:修改之后的代码在下边。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define add_edge(a,b,c) nxt[++tot]=head[a],head[a]=tot,to[tot]=b,val[tot]=c;
#define PII pair<ll,int>
#define ll long long
priority_queue<PII,vector<PII>,greater<PII> >Q;
int N,M,S,W,tot=0;
const ll inf=2147483647147483647ll;
ll val[20005]={};
int to[20005]={},head[1005]={},nxt[20005]={};
int id[1005][1005]={};
ll ans,dis[1005]={};
bool vis[1005]={};
void dij()
{
    dis[S]=0; Q.push(make_pair(0,S)); vis[S]=1;
    while(!Q.empty())
    {
        int x=Q.top().second; Q.pop();
        for(int i=head[x];i;i=nxt[i])
        {
        	if(dis[x]+val[i]<dis[to[i]])
        	{
        		dis[to[i]]=dis[x]+val[i];
        		if(!vis[to[i]])vis[to[i]]=1,Q.push(make_pair(dis[to[i]],to[i]));
        	}
        }
    }
}
int main()
{
    ll t;
    while(~scanf("%d%d%d",&N,&M,&S))
    {
        
        for(int i=1;i<=N;++i)dis[i]=inf,head[i]=0; tot=0;
        
        memset(vis,0,sizeof(vis));
        
        for(int p,q,i=1;i<=M;++i)
        {
            scanf("%d%d%lld",&p,&q,&t);
            add_edge(q,p,t);
            if(!id[q][p])id[q][p]=tot;
            else val[id[q][p]]=min(val[id[q][p]],t);
        }
        
        dij();
        
		ans=inf, scanf("%d",&W);
        for(int st,i=1;i<=W;++i)
        {
        	scanf("%d",&st);
        	ans=min(ans,dis[st]);
        }
        if(ans==inf)printf("-1\n");
        else printf("%lld\n",ans);
    }
    return 0;
}
UPD:知道WA在哪里了。
spfa写习惯了,dijkstra标记vis[]都标记错了。。
accepted 280MS 5680K
还好最后有查出来错,不然我恐怕要带着这个错的dijkstra凉凉了
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define add_edge(a,b,c) nxt[++tot]=head[a],head[a]=tot,to[tot]=b,val[tot]=c
#define PII pair<ll,int>
#define ll long long
priority_queue<PII,vector<PII>,greater<PII> >Q;
int N,M,S,W,tot=0;
ll val[20005]={};
int to[20005]={},head[1005]={},nxt[20005]={};
int id[1005][1005]={};
ll ans,dis[1005]={};
bool vis[1005]={};
void dij()
{
	for(int i=1;i<=N;++i)dis[i]=2147483647147483647ll;
    memset(vis,0,sizeof(vis));
    
    dis[S]=0; Q.push(make_pair(0,S));
    while(!Q.empty())
    {
        int x=Q.top().second; Q.pop(); vis[x]=1;
        for(int i=head[x];i;i=nxt[i])
        {
        	if(dis[x]+val[i]<dis[to[i]])
        	{
        		dis[to[i]]=dis[x]+val[i];
        		if(!vis[to[i]])Q.push(make_pair(dis[to[i]],to[i]));
        	}
        }
    }
}
int main()
{
    ll t;
    while(~scanf("%d%d%d",&N,&M,&S))
    {
        
        memset(head,0,sizeof(head)); //head要清空0~N的 ,不能只清空1~N 
        tot=0;
        memset(id,0,sizeof(id));//!!!
        
        for(int p,q,i=1;i<=M;++i)
        {
            scanf("%d%d%lld",&p,&q,&t);
            if(!id[q][p])add_edge(q,p,t),id[q][p]=tot; //add_edge不要写在外面
            else val[id[q][p]]=min(val[id[q][p]],t);
            
        }
        
        dij();
        
		ans=2147483647147483647ll;
		scanf("%d",&W);
        for(int st,i=1;i<=W;++i)
        {
        	scanf("%d",&st);
        	ans=min(ans,dis[st]);
        }
        if(ans==2147483647147483647ll)printf("-1\n");
        else printf("%lld\n",ans);
    }
    return 0;
}
对拍用的自己写的SPFA,accepted 171MS 1704K
(果然还是spfa好((
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<ctime>
#include<queue>
using namespace std;
#define ll long long
#define add_edge(a,b,c) nxt[++tot]=head[a],head[a]=tot,val[tot]=c,to[tot]=b
int N,M,S,W,tot=0;
int head[1005]={},nxt[20005]={},to[20005]={};
ll val[20005]={},dis[1005]={};
bool vis[1005]={};
queue<int>Q;
ll ans;
void SPFA()
{
	for(int i=1;i<=N;++i)dis[i]=2147483647147483647ll;
	Q.push(S); dis[S]=0;
	while(!Q.empty())
	{
		int x=Q.front(); vis[x]=0; Q.pop();
		for(int i=head[x];i;i=nxt[i])
		{
			if(dis[to[i]]>dis[x]+val[i])
			{
				dis[to[i]]=dis[x]+val[i];
				if(!vis[to[i]])vis[to[i]]=1,Q.push(to[i]);
			}
		}
	}
}
int main()
{	
	while(~scanf("%d%d%d",&N,&M,&S))
	{
		ll t; tot=0;
		ans=2147483647147483647ll;
		memset(head,0,sizeof(head));
		for(int a,b,i=1;i<=M;++i)
		{
			scanf("%d%d%lld",&a,&b,&t);
			add_edge(b,a,t);
		}
		SPFA();
		scanf("%d",&W);
		for(int st,i=1;i<=W;++i)
		{
			scanf("%d",&st);
			ans=min(dis[st],ans);
		}
		if(ans==2147483647147483647ll)printf("-1\n");
		else printf("%lld\n",ans);
	}
	return 0;
}
数据生成器
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<ctime>
#include<cctype>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
int N,M,Lim,W;
ll GenRand(const ll Lim1,ll Lim2)
{
	++Lim2;
	ll ret=Lim1;
	int t=0;
	while(t<100)
	{
		if(rand()/(RAND_MAX+1.0)<0.1)break;
		ret+=rand();
		ret%=Lim2;
		++t;
	}
	while(ret<Lim1)ret+=Lim1;
	ret%=Lim2;
	return ret;
}
bool select[1005]={};
stringstream ss;

int main( int argc, char *argv[] )
{ 
    int seed=time(NULL);
    if(argc > 1)//如果有参数
    {
        ss.clear();
        ss<<argv[1];
        ss>>seed;//把参数转换成整数赋值给seed
    }
    srand(seed);
//	scanf("%d%d%d%d",&N,&M,&Lim,&W);
	N=1000,M=GenRand(900,20000),Lim=1000,W=(int)GenRand(1,1000);
	printf("%d %d %d\n",N,M,Lim);
	ll u,v,w;
	for (int i=1;i<=M;++i) {
		u = GenRand(1,N);
		v = GenRand(1,N);
		while(v==u)v=GenRand(1,N);
		w = GenRand(1,Lim);
		printf("%lld %lld %lld\n",u,v,w);
	}
	double p;
	printf("%d\n",W);
	for (int cnt=0,i=1;cnt<W;i=(i+1)%(N+1)) {
		p=rand()/(RAND_MAX+1.0);
		if (p>0.3) {
			select[i]=1;
			++cnt;
		}
	}
	for(int i=1;i<=N;++i)if(select[i])printf("%d ",i);
	return 0;
}
比较器
@echo off

:loop

	data_generator.exe %random% > data.in
	std.exe < data.in > std.out
	my.exe < data.in > my.out

	fc my.out std.out

if not errorlevel 1 goto loop
pause

goto loop
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值