HDU-3768(最短路+dfs)

Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1309 Accepted Submission(s): 442

Problem Description
You have just moved into a new apartment and have a long list of items you need to buy. Unfortunately, to buy this many items requires going to many different stores. You would like to minimize the amount of driving necessary to buy all the items you need.

Your city is organized as a set of intersections connected by roads. Your house and every store is located at some intersection. Your task is to find the shortest route that begins at your house, visits all the stores that you need to shop at, and returns to your house.

Input
The first line of input contains a single integer, the number of test cases to follow. Each test case begins with a line containing two integers N and M, the number of intersections and roads in the city, respectively. Each of these integers is between 1 and 100000, inclusive. The intersections are numbered from 0 to N-1. Your house is at the intersection numbered 0. M lines follow, each containing three integers X, Y, and D, indicating that the intersections X and Y are connected by a bidirectional road of length D. The following line contains a single integer S, the number of stores you need to visit, which is between 1 and ten, inclusive. The subsequent S lines each contain one integer indicating the intersection at which each store is located. It is possible to reach all of the stores from your house.

Output
For each test case, output a line containing a single integer, the length of the shortest possible shopping trip from your house, visiting all the stores, and returning to your house.

Sample Input

1
4 6
0 1 1
1 2 1
2 3 1
3 0 1
0 2 5
1 3 5
3
1
2
3

Sample Output

4

题目大意是:在一个城镇中,我们要去一些商店中买东西,然后问我们去到所有商店之后并且回到家的最短路径时多少(PS:不能在中途回家)。
解题思路:因为我们是要求最短路径,并且,我们看一下数据,商店的数量也就是S是小于等于10的,那么我们可以暴力枚举去商店的顺序,在这之前,我们先求出家和商店之间的最短路径,然后我们在枚举就可以保证得到的是最小答案了。
我们可以先对每个点使用SPFA或者Dijkstra算法计算出其他商店到给点的距离,然后用maze数组建一个(s+1)*(s+1)的图就可以了(PS:S个商店和家之间的图)。
代码:SPFA

#include <bits/stdc++.h>
using namespace std;
#define int1 long long
typedef long long ll;
const int maxn=1e5+7;
const ll inf=999999999999;
struct edge
{
    ll v,w,next;
}e[maxn*3];
ll cnt,head[maxn];
void add(ll a,ll b,ll c)
{
    e[++cnt]=edge{b,c,head[a]};
    head[a]=cnt;
}
ll maze[50][50],vis[maxn],dis[maxn],ing[20],n,m;
void spfa(ll s)
{
    for(int i=0;i<=n;i++){
        dis[i]=inf,vis[i]=0;
    }
    queue<ll> q;
    q.push(s);
    dis[s]=0;
    while(!q.empty()){
        ll u=q.front();
        q.pop();vis[u]=0;
        for(int i=head[u];i;i=e[i].next){
            ll v=e[i].v,w=e[i].w;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                if(!vis[v]){
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
ll ans,s,inq[20];
void dfs(ll x,ll k,ll sum)
{
    inq[x]=1;
    if(k==s){
        ans=min(ans,sum+maze[x][0]);
        //cout<<ans<<endl;
        return ;
    }
    for(int i=1;i<=s;i++){
        if(!inq[i] && maze[x][i]<inf){
            dfs(i,k+1,sum+maze[x][i]);
            inq[i]=0;
        }
    }
}
signed main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&n,&m);
        for(int i=0;i<=n;i++){
            head[i]=0;
        }
        cnt=0;
        for(int i=0;i<15;i++){
            for(int j=0;j<15;j++){
                maze[i][j]=inf;
            }
        }
        for(int i=1;i<=m;i++){
            ll u,v,w;
            scanf("%lld%lld%lld",&u,&v,&w);
            add(u,v,w);add(v,u,w);
        }
        //int s;
        scanf("%lld",&s);
//        for(int i=0;i<=s;i++){
//            for(int j=0;j<=s;j++){
//                cout<<maze[i][j]<<' ';
//            }
//            cout<<endl;
//        }
        for(int i=1;i<=s;i++){
            scanf("%lld",ing+i);
        }
        ing[0]=0;
        for(int i=0;i<=s;i++){
            int x=ing[i];
            spfa(x);
            for(int j=0;j<=s;j++){
                int y=ing[j];
                maze[i][j]=dis[y];
            }
        }
        ans=inf;
        memset(inq,0,sizeof inq);
        dfs(0,0,0);
        printf("%lld\n",ans);
    }
    return 0;
}

dijkstra:

#include <bits/stdc++.h>
using namespace std;
#define int1 long long
typedef long long ll;
const int maxn=1e5+7;
const ll inf=999999999999;
typedef pair<ll,ll> p;
struct edge
{
    ll v,w,next;
}e[maxn*3];
ll cnt,head[maxn];
void add(ll a,ll b,ll c)
{
    e[++cnt]=edge{b,c,head[a]};
    head[a]=cnt;
}
ll maze[50][50],vis[maxn],dis[maxn],ing[20],n,m;
void dijkstra(ll s)
{
	for(int i=0;i<=n;i++){
		dis[i]=inf;vis[i]=0;
	}
    priority_queue<p,vector<p>,greater<p> > q;
    q.push(p(0,s));
    dis[s]=0;
	while(!q.empty()){
		p tmp=q.top();q.pop();
		int u=tmp.second;
		if(vis[u])continue;
		vis[u]=1;
		for(int i=head[u];i;i=e[i].next){
			int v=e[i].v,w=e[i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push(p(dis[v],v));
			} 
		}
	}
}
ll ans,s,inq[20];
void dfs(ll x,ll k,ll sum)
{
    inq[x]=1;
    if(k==s){
        ans=min(ans,sum+maze[x][0]);
        //cout<<ans<<endl;
        return ;
    }
    for(int i=1;i<=s;i++){
        if(!inq[i] && maze[x][i]<inf){
            dfs(i,k+1,sum+maze[x][i]);
            inq[i]=0;
        }
    }
}
signed main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&n,&m);
        for(int i=0;i<=n;i++){
            head[i]=0;
        }
        cnt=0;
        for(int i=0;i<15;i++){
            for(int j=0;j<15;j++){
                maze[i][j]=inf;
            }
        }
        for(int i=1;i<=m;i++){
            ll u,v,w;
            scanf("%lld%lld%lld",&u,&v,&w);
            add(u,v,w);add(v,u,w);
        }
        //int s;
        scanf("%lld",&s);
//        for(int i=0;i<=s;i++){
//            for(int j=0;j<=s;j++){
//                cout<<maze[i][j]<<' ';
//            }
//            cout<<endl;
//        }
        for(int i=1;i<=s;i++){
            scanf("%lld",ing+i);
        }
        ing[0]=0;
        for(int i=0;i<=s;i++){
            int x=ing[i];
            dijkstra(x);
            for(int j=0;j<=s;j++){
                int y=ing[j];
                maze[i][j]=dis[y];
            }
        }
        ans=inf;
        memset(inq,0,sizeof inq);
        dfs(0,0,0);
        printf("%lld\n",ans);
    }
    return 0;
}

因为我邻接表的空间开小了,T了一天,呜呜呜。

发布了34 篇原创文章 · 获赞 3 · 访问量 242

猜你喜欢

转载自blog.csdn.net/qq_44641782/article/details/103275745
今日推荐