1. 程式人生 > >[HDU2680] Choose the best route [單源最短路]

[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