题目链接:http://lightoj.com/volume_showproblem.php?problem=1316
分析分析:大意就是一个人要从0点走到n-1点,路中有些点有商店可以买礼物,所以这个人想买多点礼物,但是又要尽快的走到n-1点,这两者之间呢,礼物的多少优先,其次是路程长度。
s <= 16.16个商店走哪些可以用状压解决,但是要用二维的状压。因为你要知道这些闪电走完之后最后停留在哪个商店的。
所以dp[statu][i]statu状态走完后停留在第i个商店,位置时shop[i]。然后转移还是挺简单的,我用的宽搜。
显然转移中需要用到商店之间的最小距离,所以我们要先求一个任意两点的最短距离,这里不能用floyd,会超时。
只能是枚举每个商店为源点,跑spfa或者是dijk。
跑完之后再判断下连通性,可不可以从0走到n-1。
再说个辛酸的事儿吧,,,由于开始spfa写挂了,,,wa了7发,都开始怀疑人生了。。。。。。
太久不练题,,,生疏了。
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-10; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; /********************************************************/ /**********************Point*****************************/ /********************************************************/ struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y){} }; typedef Point Vector; Vector operator + (Vector A,Vector B){ return Vector(A.x + B.x,A.y + B.y); } Vector operator - (Vector A,Vector B){//向量减法 return Vector(A.x - B.x,A.y - B.y); } Vector operator * (Vector A,double p){//向量数乘 return Vector(A.x * p,A.y * p); } Vector operator / (Vector A,double p){//向量除实数 return Vector(A.x / p,A.y / p); } int dcmp(double x){//精度正负、0的判断 if (fabs(x) < eps) return 0; return x < 0?-1:1; } bool operator < (const Point& A,const Point& B){//小于符号的重载 return A.x < B.x || (A.x == B.x && A.y < B.y); } bool operator == (const Point& A,const Point& B){//点重的判断 return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0; } double Dot(Vector A,Vector B){//向量的点乘 return A.x * B.x + A.y * B.y; } double Length(Vector A){//向量的模 return sqrt(Dot(A,A)); } double Angle(Vector A,Vector B){//向量的夹角 return acos(Dot(A,B) / Length(A) / Length(B)); } double Cross(Vector A,Vector B){//向量的叉积 return A.x * B.y - A.y * B.x; } double Area2(Point A,Point B,Point C){//三角形面积 return Cross(B - A,C - A); } Vector Rotate(Vector A,double rad){//向量的旋转 return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad)); } Vector Normal(Vector A){//法向量 int L = Length(A); return Vector(-A.y / L,A.x / L); } double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离 Vector v1 = B - A,v2 = p - A; return fabs(Cross(v1,v2)) / Length(v1); } double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离 if (A == B) return Length(p - A); Vector v1 = B - A, v2 = p - A,v3 = p - B; if (dcmp(Dot(v1,v2 < 0))) return Length(v2); else if (dcmp(Dot(v1,v3)) > 0) return Length(v3); else return DistanceToLine(p,A,B); } Point GetLineProjection(Point p,Point A,Point B){//投影 Vector v = B - A; return A + v * (Dot(v, p - A) / Dot(v, v)); } bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交 double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1); double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1); return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0; } bool OnSegment(Point p,Point A,Point B){//点在线段上 return dcmp(Cross(A - p,B - p)) == 0 && dcmp(Dot(A - p,B - p)) < 0; } double PolygonArea(Point* p,int n){//多边形面积 double area = 0; for (int i = 1;i < n - 1;++i){ area += Cross(p[i] - p[0],p[i + 1] - p[0]); } return area / 2; } double PolygonArea(vector<Point> p){//多边形面积 Point O(0,0); double ret = 0; p.push_back(p[0]); for (int i = 0;i < p.size() - 1;++i){ ret += Cross(p[i] - O,p[i + 1] - O); } return ret / 2.0; } /********************************************************/ /**********************Line******************************/ /********************************************************/ struct Line{ Point p;//直线上任意一点 Vector v;//方向向量,它的左边就是半平面 double ang;//极角 Line(){} // Line(Point p,Vector v):p(p),v(v){} Line(Point A,Point B):p(A){ v = B - A; ang = atan2(v.y,v.x); } bool operator < (const Line& rhs)const{ return ang < rhs.ang; } }; const int maxn = 510; const int maxm = 10010; bool vis[maxn]; vector<ii> G[maxn]; inline void SPFA(int st,int d[]){ queue<int> que; que.push(st); memset(vis, false,sizeof vis); d[st] = 0; while(!que.empty()){ int u = que.front(); que.pop(); vis[u] = false; for (int i = 0;i < G[u].size();++i){ int v = G[u][i].first; int w = G[u][i].second; if (d[v] > d[u] + w){ d[v] = d[u] + w; if (!vis[v]){ vis[v] = true; que.push(v); } } } } } int n, m, s; int shop[20]; int dis[maxn][maxn]; int dp[1 << 16][16];//状态,点 inline void Input(){ scanf("%d%d%d",&n,&m,&s); for (int i = 0;i < s;++i) scanf("%d",&shop[i]); for (int i = 0;i < n;++i) G[i].clear(); int u, v, w; memset(dis, INF, sizeof dis); for (int i = 0;i < m;++i){ scanf("%d%d%d",&u,&v,&w); if (dis[u][v] > w) dis[u][v] = w; } for (int i = 0;i < n;++i){ for (int j = 0;j < n;++j){ if (dis[i][j] < INF) G[i].push_back(ii(j, dis[i][j])); } } memset(dis, INF, sizeof dis); } int OneBit(int x){ return x?OneBit(x / 2) + x % 2:0; } inline void SOLVE(){ SPFA(0, dis[0]); for (int i = 0;i < s;++i){ SPFA(shop[i], dis[shop[i]]); // printf("st = %d\n", shop[i]); // for (int j = 0;j < n;++j) // printf("%12d", dis[shop[i]][j]); // printf("\n"); } if (dis[0][n - 1] == INF){ puts("Impossible"); }else{ queue<ii> que; memset(dp, INF, sizeof dp); for (int i = 0;i < s;++i){ dp[1 << i][i] = dis[0][shop[i]]; que.push(ii(1 << i, i)); } while(!que.empty()){ ii tmp = que.front(); que.pop(); int u = tmp.second;// int stc = tmp.first; if (dp[stc][u] >= INF) continue; for (int i = 0;i < s;++i){ if (!(stc & (1 << i)) && dis[shop[u]][shop[i]] < INF){ if (dp[stc][u] + dis[shop[u]][shop[i]] < dp[stc | (1 << i)][i]){ dp[stc | (1 << i)][i] = dp[stc][u] + dis[shop[u]][shop[i]]; que.push(ii(stc | (1 << i), i)); } } } } int MaxNum = -INF, MinDis = INF; for (int i = 0;i < (1 << s);++i){ int num = OneBit(i); if (MaxNum == num){ for (int j = 0;j < s;++j){ if (i & (1 << j) && dp[i][j] < INF && dis[shop[j]][n - 1] < INF) MinDis = min(MinDis, dp[i][j] + dis[shop[j]][n - 1]); } }else if (MaxNum < num){ int first = true; for (int j = 0;j < s;++j){ if (i & (1 << j) && dp[i][j] < INF && dis[shop[j]][n - 1] < INF){ if (first){ MaxNum = num; first = false; MinDis = dp[i][j] + dis[shop[j]][n - 1]; }else MinDis = min(MinDis, dp[i][j] + dis[shop[j]][n - 1]); } } } } if (MaxNum == -INF) puts("Impossible"); else printf("%d %d\n", MaxNum, MinDis); } } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int t, icase = 0; scanf("%d",&t); while(t--){ printf("Case %d: ", ++icase); Input(); SOLVE(); } return 0; }