程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU3768 Shopping(狀態壓縮DP+spfa)旅行商問題

HDU3768 Shopping(狀態壓縮DP+spfa)旅行商問題

編輯:C++入門知識

HDU3768 Shopping(狀態壓縮DP+spfa)旅行商問題


Shopping

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 577 Accepted Submission(s): 197



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

Source University of Waterloo Local Contest 2010.07.10
題意:給出n個點,m條路,s(1<=s<=10)個店位於n個地方,現在要求從0點出發走完所有的店的最小花費。 解題:先求出店與店之間的最短路,包括出發點0,然後就是狀態壓縮dp。
#include
#include
#include
using namespace std;
#define mov(a) (1<<(a))
const int N = 100005;
const int inf = 99999999;
struct EDG{
    int v,c;
};

int dis[N],inq[N],mark[N],n,road[15][15],dp[mov(12)][12];
vectormapt[N];

void spfa(int s)
{
    queueq;
    for(int i=0;i<=n;i++)
        dis[i]=inf,inq[i]=0;
    dis[s]=0;
    q.push(s);
    while(!q.empty())
    {
        s=q.front(); q.pop();
        inq[s]=0;
        int k=mapt[s].size();
        for(int i=0;idis[s]+mapt[s][i].c)
            {
                dis[v]=dis[s]+mapt[s][i].c;
                if(inq[v]==0)
                    inq[v]=1,q.push(v);
            }
        }
    }
}
int main()
{
    int t,m,s,a,b,store[15];
    EDG ss;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i=0)
                road[i][mark[j]]=dis[j];
        }
        
        for(int sta=0;stadp[sta][i]+road[i][j])
                    dp[sta|mov(j)][j]=dp[sta][i]+road[i][j];
            }
        }
        int ans=inf;
        for(int i=0;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved