您的位置:首页 > 编程语言

编程练习A(回路问题)

2016-11-29 00:30 134 查看

A:Jogging Trails(POJ 2404)

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

Description

Gord is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Gord wants to find the shortest jogging route that travels along every trail at least once.

Input

Input consists of several test cases. The first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and n, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord’s route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.

Output

For each case, there should be one line of output giving the length of Gord’s jogging route.

Sample Input

4 5

1 2 3

2 3 4

3 4 5

1 4 10

1 3 12

0

Sample Output

41

我的代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int INF = 0xffffff;
int T,n,m;
int i,j,k;
int a,b,len;
int Map[20][20];
int du[20];
int dp[1<<16];
int stat;
int sum;
inline int min(int a, int b){
return a > b?b:a;
}
void init(){
memset(du, 0, sizeof(du));
memset(dp, 0, sizeof(dp));
stat = 0;
sum = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == j) {
Map[i][j] = 0;
}else{
Map[i][j] = INF;
}
}
}

}

void floyd(){
for (k = 0; k <n; k++) {
for (i = 0; i <n; i++) {
for (j = 0; j < n; j++) {
if (i==j||j==k||i==k) continue;
Map[i][j] = min(Map[i][j], Map[i][k]+ Map[k][j]);
}
}
}
}

int DP(int s){
if(s == 0){
return 0;
}
if(dp[s]){
return dp[s];
}
int ans = INF;
for (i = 0; i <n; i++) {
if (s & (1 << i)) {
for (j = i+1; j < n; j++) {
if ( s&(1 << j)) {
int temp = DP(s-(1<<i)-(1<<j)) + Map[i][j];
ans = min(ans, temp);
}
}
}
}
return dp[s] = ans;
}
void insert(int a, int b, int length) {
if (len < Map[a][b]) {
Map[a][b] = Map[b][a] = length;
}
sum +=length;
du[a]++;
du[b]++;
}
int main() {
freopen("input.txt", "r", stdin);

while(scanf("%d",&n)!=EOF) {
if (n == 0) {
break;
}
scanf("%d", &m);
init();
for (i = 0; i < m; i++) {
scanf("%d%d%d",&a,&b,&len );
insert(a-1,b-1,len);
}
floyd();
for (i = 0; i <n; i++) {
if (du[i] % 2 == 1) {
stat |= (1<< i);
}
}
//    printf("%d %x\n", sum,stat);
sum += DP(stat);
printf("%d\n", sum);
}
return 0;
}


B:Colored Sticks(POJ 2513)

Time Limit:5000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu

Description

You are given a bunch of wooden sticks. Each endpoint of each stick is colored with
4000
some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

Input

Input is a sequence of lines, each line contains two words, separated by spaces, giving the colors of the endpoints of one stick. A word is a sequence of lowercase letters no longer than 10 characters. There is no more than 250000 sticks.

Output

If the sticks can be aligned in the desired way, output a single line saying Possible, otherwise output Impossible.

Sample Input

blue red

red violet

cyan blue

blue magenta

magenta cyan

Sample Output

Possible

Hint

Huge input,scanf is recommended.

我的代码

//两个非常恼火的错误。1.Union里面更新的时候
//1.fa[a] = fa[b],写成了fa[a] = b;
//2.main函数里面忘记加init了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 510000;
int color_count;
int du[maxn];
int fa[maxn];
int Rank[maxn];
struct trie_node{
int color;
trie_node *next[26];
trie_node(){
color = 0;
memset(next, 0, sizeof(next));
}
};
trie_node *root = new trie_node;
void init(){
color_count = 0;
memset(du, 0, sizeof(du));
for (int i = 0; i < maxn; i++) {
fa[i] = i;
Rank[i] = 1;
}
}
void del(trie_node* p){
for (int i =0; i < 26; i++) {
if (p->next[i]!=NULL) {
del(p->next[i]);
}
}
delete(p);
}

int insert(trie_node *p, char *str){
for (int i = 0; str[i] != '\0'; i++) {
int num = str[i] - 'a';
if (p->next[num] == NULL) {
p->next[num] = new trie_node;
p = p->next[num];
}else{
p = p->next[num];
}
}
if (p->color == 0) {
p->color = ++color_count;
}
du[p->color] ++;
return p->color;
}

int find(int a){
if (fa[a] != a) {
fa[a] = find(fa[a]);
}
return fa[a];
}

void Union(int a, int b){
if (Rank[a] < Rank[b]) {
fa[a] = fa[b];
}else if(Rank[a] > Rank[b]){
fa[b] = fa[a];
}else{
fa[b] = fa[a];
Rank[a]++;
}
}
bool connected(){
for (int i = 1; i < color_count; i++) {
if (find(i) != find(i+1)) {
return false;
}
}
return true;
}

void input(){
int a, b;
char  str1[15], str2[15];
while(scanf("%s%s", str1, str2)!=EOF){
a = insert(root, str1);
b = insert(root, str2);
Union(a, b);
}
}

int main() {
// freopen("input.txt", "r", stdin);
init();
input();
if(!connected()){
printf("Impossible\n");
}else{
int x = 0;
for (int i = 1; i <= color_count; i++) {
if (du[i] % 2 ==1) {
x++;
}
}
if (x > 2) {
printf("Impossible\n");
}else{
printf("Possible\n");
}
}
// del(root);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编程