您的位置:首页 > 其它

LA 4256

2014-04-27 20:47 323 查看
Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location to the company on a regular basis. They also have to report their new location to the company if they are moving to another location. The company keep each salesman's working path on a map of his working area and uses this path information for the planning of the next work of the salesman. The map of a salesman's working area is represented as a connected and undirected graph, where vertices represent the possible locations of the salesman an edges correspond to the possible movements between locations. Therefore the salesman's working path can be denoted by a sequence of vertices in the graph. Since each salesman reports his position regularly an he can stay at some place for a very long time, the same vertices of the graph can appear consecutively in his working path. Let a salesman's working path be correct if two consecutive vertices correspond either the same vertex or two adjacent vertices in the graph.For example on the following graph representing the working area of a salesman,
#include <iostream>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX_N = 205;
const int edge = 5000;
int N,M;
int a[MAX_N];
int dp1[MAX_N],dp2[MAX_N];
bool f[MAX_N][MAX_N];
int n;

void solve() {
int *now = dp2,*last = dp1;
for(int i = 1; i <= n; ++i) {
fill(now + 1,now + N + 1,n + 1);
for(int j = 1; j <= N; ++j) {
int v = a[i] != j;
for(int k = 1; k <= N; ++k) {
if(!f[j][k]) continue;
if(last[k] != n + 1)
now[j] = min(now[j],last[k] + v);
}

}
swap(now,last);
}

int ans = n + 1;
//for(int i = 1; i  <= N; ++i) printf("%d",last[i]);
//printf("\n");
for(int i = 1; i <= N; ++i) ans = min(ans,last[i]);
printf("%d\n",ans);
}

int main()
{
// freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&N,&M);

memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
memset(f,0,sizeof(f));
for(int i = 1; i <= N; ++i) f[i][i] = 1;

for(int i = 0; i <  M; ++i) {
int u,v;
scanf("%d%d",&u,&v);
f[u][v] = f[v][u] = 1;

}
scanf("%d",&n);
for(int i = 1; i <= n; ++i) {
scanf("%d",&a[i]);
}

solve();

}

return 0;
}
View Code

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: